Range Over Channels in Erlang
Here’s the Erlang translation of the “Range over Channels” example:
In a previous example, we saw how list comprehensions provide iteration over basic data structures. We can also use similar syntax to iterate over values received from a process.
To run the program:
This example also showed that it’s possible to terminate a process that still has remaining values to be received, but still have those remaining values be received before the receiving process terminates.
In Erlang, we don’t have channels as in some other languages, but we can achieve similar behavior using processes and message passing. The queue
function acts as our channel, sending messages one at a time when requested. The receive_loop
function acts as our range, receiving messages until the sending process terminates.