Introducing Erlang by Simon St.Laurent

Introducing Erlang by Simon St.Laurent

Author:Simon St.Laurent
Language: eng
Format: epub, mobi
Publisher: O'Reilly Media, Inc.
Published: 2017-02-28T05:00:00+00:00


Example 8-4. Using the return value of the receive clause as state for the next iteration

-module(bounce). -export([report/1]). report(Count) -> NewCount = receive X -> io:format("Received #~p: ~p~n",[Count,X]), Count + 1 end, report(NewCount).

In this model, all (though just one here) of the receive clauses return a value that gets passed to the next iteration of the function. If you use this approach, you can think of the return value of the receive clause as the state to be preserved between function calls. That state can be much more intricate than a counter—it might be a tuple, for instance, that includes references to important resources or work in progress.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.