Created
April 23, 2017 18:56
-
-
Save nsomar/a4d783c617d47dbac35a0714e0b789c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 Storage do | |
| def start(initial) do | |
| process = spawn_link(fn -> execute(initial) end) | |
| Process.register(process, Storage) | |
| process | |
| end | |
| def execute(state) do | |
| receive do | |
| {:get, sender} -> | |
| send(sender, state) | |
| execute(state) | |
| {:put, val} -> | |
| execute(state ++ [val]) | |
| end | |
| end | |
| def put(value) do | |
| send(Storage, {:put, value}) | |
| end | |
| def get do | |
| send(Storage, {:get, self()}) | |
| receive do | |
| msg -> msg | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment