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
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!
Marked As Solved
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
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]}
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








