Created
July 25, 2017 15:26
-
-
Save tamanugi/028c49ac76dc12c44dd26ebe26b8c225 to your computer and use it in GitHub Desktop.
[Elixir]キューを実装してみた ref: http://qiita.com/tamanugi/items/85072766377fc6475935
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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