quda

quda

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!

Marked As Solved

dorgan

dorgan

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).

Also Liked

dorgan

dorgan

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]}

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New

We're in Beta

About us Mission Statement