Skip to content

Instantly share code, notes, and snippets.

@tamanugi
Created July 25, 2017 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamanugi/028c49ac76dc12c44dd26ebe26b8c225 to your computer and use it in GitHub Desktop.
Save tamanugi/028c49ac76dc12c44dd26ebe26b8c225 to your computer and use it in GitHub Desktop.
[Elixir]キューを実装してみた ref: http://qiita.com/tamanugi/items/85072766377fc6475935
iex(1)> QueueEx.Queue.start_link
{:ok, #PID<0.124.0>}
iex(2)> QueueEx.Queue.add 1
:ok
iex(3)> QueueEx.Queue.add 2
:ok
iex(4)> QueueEx.Queue.add 3
:ok
iex(5)> QueueEx.Queue.add 3
:ok
iex(6)> QueueEx.Queue.pop
1
iex(7)> QueueEx.Queue.pop
2
iex(8)> QueueEx.Queue.pop
3
defmodule Queue do
@name __MODULE__
def start_link, do: Agent.start_link(fn -> [] end, name: @name)
def add(job), do: Agent.update(@name, fn queue -> queue ++ [job] end)
def pop, do: Agent.get_and_update(@name, fn [head | tail] -> {head, tail})
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment