That will guard you from many lines of repetitive code!<br><br><div class="gmail_quote">On Fri, May 6, 2011 at 13:12, Ivan Uemlianin <span dir="ltr"><<a href="mailto:ivan@llaisdy.com">ivan@llaisdy.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Thanks!  I knew there would be something.  Time for me to start using guards.<br>
<br>
Best wishes<br>
<br>
Ivan<div><div></div><div class="h5"><br>
<br>
<br>
On 06/05/2011 11:54, Torben Hoffmann wrote:<br>
</div></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div></div><div class="h5">
-module(ivan).<br>
<br>
-compile(export_all).<br>
<br>
<br>
process(This,That,State) -><br>
<br>
    receive<br>
    {From, do1} -><br>
        From ! do1,<br>
        process(This,That,State);<br>
    {From, do2 } -><br>
        From ! do2,<br>
        process(This,That,State);<br>
    {From, do3} when State=:=a -><br>
        From ! do3,<br>
        process(This,That,b);<br>
    {From, do4} when State=:=b -><br>
        From ! do4,<br>
        process(This,That,a)<br>
    end.<br>
<br>
listen(P) -><br>
    receive<br>
    {send,M} -><br>
        P ! {self(),M},<br>
        listen(P);<br>
    M -><br>
        io:format("listen got: ~p~n",[M]),<br>
        listen(P)<br>
    end.<br>
<br>
Cheers,<br>
Torben<br>
<br>
<br></div></div><div><div></div><div class="h5">
On Fri, May 6, 2011 at 11:31, Ivan Uemlianin <<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a> <mailto:<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>>> wrote:<br>

<br>
    Dear All<br>
<br>
    % I have a process running continuously in a receive loop.  It<br>
    should act slightly differently depending on one of its state<br>
    parameters.  At the moment I have it like this:<br>
<br>
    process(This, That, a) -><br>
       receive<br>
           {From, do1} -><br>
               do1,<br>
               process(This, That, a);<br>
           {From, do2} -><br>
               do2,<br>
               process(This, That, a);<br>
           {From, do3} -><br>
               do3,<br>
               process(This, That, a)<br>
       end;<br>
<br>
    process(This, That, b) -><br>
       receive<br>
           {From, do1} -><br>
               do1,<br>
               process(This, That, b);<br>
           {From, do2} -><br>
               do2,<br>
               process(This, That, b);<br>
           {From, do4} -><br>
               do4,<br>
               process(This, That, b)<br>
       end.<br>
<br>
<br>
    % Most of the receive clauses are common to both versions of<br>
    process/3 (the real code is more complicated).  Is there a way to<br>
    abstract out the common parts?  An "object oriented" language<br>
    might use inheritance.  Prolog might use backtracking, perhaps a<br>
    bit like this:<br>
<br>
    iProcess(This, That, a) -><br>
       receive<br>
           {From, do3} -><br>
               do3,<br>
               iProcess(This, That, a)<br>
       end;<br>
<br>
    iProcess(This, That, b) -><br>
       receive<br>
           {From, do4} -><br>
               do4,<br>
               iProcess(This, That, b)<br>
       end;<br>
<br>
    iProcess(This, That, Other) -><br>
       receive<br>
           {From, do1} -><br>
               do1,<br>
               iProcess(This, That, Other);<br>
           {From, do2} -><br>
               do2,<br>
               iProcess(This, That, Other)<br>
       end.<br>
<br>
<br>
    % A case would perhaps work, with the proviso that 'generic'<br>
    receives would be evaluated first:<br>
<br>
    cProcess(This, That, Other) -><br>
       receive<br>
           {From, do1} -><br>
               do1,<br>
               cProcess(This, That, Other);<br>
           {From, do2} -><br>
               do2,<br>
               cProcess(This, That, Other)<br>
       end,<br>
       case Other of<br>
           a -><br>
               receive<br>
                   {From, do3} -><br>
                       do3,<br>
                       cProcess(This, That, Other)<br>
               end;<br>
           b -><br>
               receive<br>
                   {From, do4} -><br>
                       do4,<br>
                       cProcess(This, That, Other)<br>
               end<br>
       end.<br>
<br>
<br>
    % Is there a canonical way of dealing with this problem (other<br>
    than copy-and-paste)?<br>
<br>
    With thanks and best wishes<br>
<br>
    Ivan<br>
<br>
    --     ============================================================<br>
    Ivan A. Uemlianin<br>
    Speech Technology Research and Development<br>
<br></div></div>
    <a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a> <mailto:<a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a>><br>
    <a href="http://www.llaisdy.com" target="_blank">www.llaisdy.com</a> <<a href="http://www.llaisdy.com" target="_blank">http://www.llaisdy.com</a>><br>
    <a href="http://llaisdy.wordpress.com" target="_blank">llaisdy.wordpress.com</a> <<a href="http://llaisdy.wordpress.com" target="_blank">http://llaisdy.wordpress.com</a>><div class="im"><br>
    <a href="http://www.linkedin.com/in/ivanuemlianin" target="_blank">www.linkedin.com/in/ivanuemlianin</a><br></div>
    <<a href="http://www.linkedin.com/in/ivanuemlianin" target="_blank">http://www.linkedin.com/in/ivanuemlianin</a>><div class="im"><br>
<br>
       "Froh, froh! Wie seine Sonnen, seine Sonnen fliegen"<br>
                        (Schiller, Beethoven)<br>
    ============================================================<br>
<br>
    _______________________________________________<br>
    erlang-questions mailing list<br></div>
    <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a> <mailto:<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a>><div class="im">
<br>
    <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
<br>
<br>
<br>
<br>
-- <br>
<a href="http://www.linkedin.com/in/torbenhoffmann" target="_blank">http://www.linkedin.com/in/torbenhoffmann</a><br>
</div></blockquote>
<br><div><div></div><div class="h5">
<br>
-- <br>
============================================================<br>
Ivan A. Uemlianin<br>
Speech Technology Research and Development<br>
<br>
                    <a href="mailto:ivan@llaisdy.com" target="_blank">ivan@llaisdy.com</a><br>
                     <a href="http://www.llaisdy.com" target="_blank">www.llaisdy.com</a><br>
                         <a href="http://llaisdy.wordpress.com" target="_blank">llaisdy.wordpress.com</a><br>
                     <a href="http://www.linkedin.com/in/ivanuemlianin" target="_blank">www.linkedin.com/in/ivanuemlianin</a><br>
<br>
    "Froh, froh! Wie seine Sonnen, seine Sonnen fliegen"<br>
                     (Schiller, Beethoven)<br>
============================================================<br>
<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.linkedin.com/in/torbenhoffmann">http://www.linkedin.com/in/torbenhoffmann</a><br>