in-ram lists

Taavi Talvik <>
Fri Mar 24 15:11:20 CET 2006


On Mar 24, 2006, at 3:26 PM, Damir Horvat wrote:

> On Fri, Mar 24, 2006 at 01:48:06PM +0100, R?mi H?rilier wrote:
>
>> Don't forget to get the value returned by queue:in(Item,Queue).
>>
>> 1> Q1 = queue:new().
>> {[],[]}
>> 2> Q2 = queue:in("item1", Q1).
>> {["item1"],[]}
>> 3> Q3 = queue:in("item2", Q2).
>> {["item2"],["item1"]}
>> 4>
>
> Ok, I get this. But what bothers me is, how to do this in functional
> way?

Why in functional way? Try erlang way;)
	
Untested code follows:

% Creates new queue (creates erlang proccess), Retuns pid, which will 
used as queue identifier
new_queue() ->
     Q = queue:new(),
     Pid = spwan(?MODULE, queue_manager, [Q]),
     Pid.

push(element, Queue) ->
         Queue ! {push, element}.

shift(Queue) ->
     Queue ! shift,
     receive
         Response ->
             Response
     end.

queue_manager(Q) ->
     receive
         {push, Element} ->
             Q2 = queue:in(Element, Q),
             queue_manager(Q2);
         {Pid,shift} ->
             {Result, Q2} = queue:out(Q)
             Pid ! Result,
             queue_manager(Q2)
     end.

best regards,
taavi



More information about the erlang-questions mailing list