Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to use function call in function guard
    primarykey
    data
    text
    <p>I'm new to Erlang and am trying to program a bounded-buffer problem program. It is almost working, except for making sure the producers don't get too far ahead and overwrite unconsumed data. To handle this, I decided to try putting guards on my buffer() function so that I could have a version w/o receive used when the buffer is full, a version w/o send used when the buffer is empty, and a normal version for the rest of the time.</p> <p>My problem is that the guard for the receiver-less version requires me to know the size of the array representing the buffer, which requires a call to <code>array:size/1</code>. Apparently, Erlang does not allow function invocations in guards, which prevents this from working. Is there some way to work around this without changing the function declaration for my buffer actor?</p> <pre><code>%% buffer: array num num %% A process that holds the shared buffer for the producers and consumers buffer(Buf, NextWrite, NextRead) when NextWrite == NextRead -&gt; io:format(" * ~w, ~w, ~w~n", [array:to_list(Buf), NextRead, NextWrite]), receive {enqueue, Reply_Pid, Num} -&gt; io:format("~w: &gt; ~w~n", [Reply_Pid, Num]), buffer(array:set(NextWrite rem array:size(Buf), Num, Buf), NextWrite + 1, NextRead); finish -&gt; io:format("finished printing~n") end; buffer(Buf, NextWrite, NextRead) when (NextWrite - NextRead) == array:size(Buf) -&gt; io:format(" * ~w, ~w, ~w~n", [array:to_list(Buf), NextRead, NextWrite]), receive {dequeue, Reply_Pid} -&gt; io:format("~w: &lt; ~w~n", [Reply_Pid, array:get(NextRead rem array:size(Buf), Buf)]), Reply_Pid ! {reply, array:get(NextRead rem array:size(Buf), Buf)}, buffer(Buf, NextWrite, NextRead + 1); finish -&gt; io:format("finished printing~n") end; buffer(Buf, NextWrite, NextRead) -&gt; io:format(" * ~w, ~w, ~w~n", [array:to_list(Buf), NextRead, NextWrite]), receive {dequeue, Reply_Pid} -&gt; io:format("~w: &lt; ~w~n", [Reply_Pid, array:get(NextRead rem array:size(Buf), Buf)]), Reply_Pid ! {reply, array:get(NextRead rem array:size(Buf), Buf)}, buffer(Buf, NextWrite, NextRead + 1); {enqueue, Reply_Pid, Num} -&gt; io:format("~w: &gt; ~w~n", [Reply_Pid, Num]), buffer(array:set(NextWrite rem array:size(Buf), Num, Buf), NextWrite + 1, NextRead); finish -&gt; io:format("finished printing~n") end. </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload