Help for a simple queue wrapper

I need to implement a (simple) queue wrapper on the Erlan :queue, as Elixir does not have something similar :frowning_face: 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
{, } #:sob::sob::sob: 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!

The code in your module is ok, but in Elixir data is immutable, each time you add or remove an item from the queue, a new queue is returned, so you have to rebind q to the result of Fifo.*

iex(2)> q = Fifo.new
{[], []}
iex(3)> q
{[], []}
iex(4)> q = Fifo.in!(2, q)
{[2], []}
iex(5)> q
{[2], []}
iex(6)> Fifo.in!("r", q)
{["r"], [2]}
iex(7)> q = Fifo.new
{[], []}
iex(8)> q = Fifo.in!(3, q)
{[3], []}
iex(9)> q = Fifo.in!(2, q)
{[2], [3]}
iex(10)> q = Fifo.in!("r", q)
{["r", 2], [3]}
iex(11)> q
{["r", 2], [3]}

Also bear in mind that erlang usually has the data in the last argument(common in many other functional languages, it is useful when using currying and partial function application), but in elixir it’s more common to have the data(in this case the queue) in the first argument, so you can leverage the pipe |> operator:

defmodule Fifo do
  def new do
    :queue.new()
  end

  def in!(q, x) do
   :queue.in(x, q)
  end

  def out!(q) do
    :queue.out(q)
  end
end

q =
  Fifo.new()
  |> Fifo.in!(3)
  |> Fifo.in!(2)
  |> Fifo.in!("r")
#=> {["r", 2], [3]}
3 Likes

Thanks for the suggestion, but the rebounding thing doesn’t solve the problem, it’s exactly what I wanted to avoid, otherwise my wrapper is identical with the Erlang queue implementation.
Is there a way to maintain the state my queue state inside the module ? Such as in OOP, using something as this queue = queue ?
The pipe doesn’t help because when I will further use the wrapper I would have to do other operations between the pipes… such as: queue in something, then process something, dequeue, loop, so on.

No, you can’t avoid rebinding, there’s no implicit mutable state, there are no stateful objects in functional land. Modules are not like classes, they’re just a bag of functions. Everything you do in the language will behave like that. This requires you to approach problems with a different mindset and you should not try to force OOP patterns here.

If you want to keep the state and “mutate” it over time, then you can spawn a genserver and keep the queue as it’s state. Check the elixir guide to see an example of a stateful Key-Value store.

q = Fifo.new() |> Fifo.in!(1)

# do something else

q = Fifo.in!(q, 2)

There’s no other way around it, the whole language works this way, it has nothing to do with the way the queue is implemented in erlang(it’s just a data structure).

3 Likes

thanks @dorgan. You’re right, it’s my mindset. It’s extremely difficult to drop the OOP pattern world. I’ve have still a lot to grasp in FP (I always lose sight that everything is immutable in this paradigm)