DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Julian has posted 1 posts at DZone. View Full User Profile

A Silly FIFO In Erlang (you Should Use OTP's Queue.erl Instead)

10.01.2006
| 4502 views |
  • submit to reddit
        
-module(fifo).
-export([new/0, loop/0, push/2, pop/1]).

new() ->
    spawn(?MODULE, loop, []).

loop() ->
    receive
        {Pid,pop} ->
            Pid ! {self(),receive {push,X} ->
                                  X
                          end),
            loop()
    end.

push(Fifo,X) ->
    Fifo ! {push,X},
    X.

pop(Fifo) ->
    Fifo ! {self(),pop},
    receive {Fifo,X} ->
            X
    end.