<span style="font-family:courier new,monospace">-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></span><br>Cheers,<br>Torben<br><br><br><div class="gmail_quote">On Fri, May 6, 2011 at 11:31, Ivan Uemlianin <span dir="ltr"><<a href="mailto:ivan@llaisdy.com" target="_blank">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">
Dear All<br>
<br>
% I have a process running continuously in a receive loop.  It should act slightly differently depending on one of its state 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 process/3 (the real code is more complicated).  Is there a way to abstract out the common parts?  An "object oriented" language might use inheritance.  Prolog might use backtracking, perhaps a 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' 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 than copy-and-paste)?<br>
<br>
With thanks and best wishes<br>
<br>
Ivan<br>
<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>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><a href="http://www.linkedin.com/in/torbenhoffmann" target="_blank">http://www.linkedin.com/in/torbenhoffmann</a><br>