I need to implement a (simple) queue wrapper on the Erlan :queue, as Elixir does not have something similar
and this Erlang implementation of queue is not handy to use.
I warn that I have little experience with Elixir and fp in general, I am coming from the OOP world - classes, objects, this, self, closure etc.
I wrote this:
defmodule Fifo do
def new do
:queue.new()
end
def in!(x, q) do
:queue.in(x, q)
end
def out!(q) do
:queue.out(q)
end
end
Result => disaster!
q = Fifo.new
{, } # As expected
Then:
Fifo.in!(3, q)
{[3], } # As expected as well
Then came the unexpected:
iex(26)> Fifo.in!(2, q)
{[2], } # Whoops! I expected {[2,3]}
iex(27)> Fifo.in!(“r”, q)
{[“r”], } # expected {[“r”,2,3]}
The disaster completes:
iex(28)> q
{, } #My queue q is empty!!
There’s clearly something wrong with my implementation, I needed some sort of closure; “this” does not exist in Elixir; the state is not recorded, my variable q is lost on the way.
Please… how to do this ? Help!


















