in-ram lists

Ryan Rawson ryanobjc@REDACTED
Fri Mar 24 20:06:41 CET 2006


Recursion solves all your problems, along with processes.  Eg:

queue_loop( Q ) ->
 receive
     { push, Datum } ->
              queue_loop( queue:in( Q, Datum ) ) ;
     { pop, From } ->
              {Datum, NewQ } = queue:out( Q ) ,
              From ! Datum ,
              queue_loop( NewQ )
   end.

You get around having to name successive queues different names, yet
you get the behaviour you want.

Since erlang processes are light weight, you can have a process for
every queue.  The queue ID is now just a process id - this is how the
file:open() and io:format() functions work.

good luck,
-ryan

On 3/24/06, orbitz@REDACTED <orbitz@REDACTED> wrote:
> You can always write a process to handle this stuff.  If you "don't
> want to worry about naming variables to capture return values" then
> perhaps you are using the wrong language paradigm.
>
> On Mar 24, 2006, at 8:26 AM, 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?
> >
> > I'd like to have this queue accessible in ram. I'd like to have two
> > functions (push, shift) which adds new element to the queue and takes
> > one off. I don't want to worry about naming variables to capture return
> > values.
> >
> > For example:
> >
> > push (element, queue) # puts element on the tail of the queue
> > shift (queue) # gets first element from the queue.
> >
> > That's all I need. Is this doable in erlang?
> >
>
>



More information about the erlang-questions mailing list