Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Calculator do
- def start do
- spawn(fn -> loop(0) end)
- end
- defp loop(cur_val) do
- result = receive do
- {:value, sender_id} -> send(sender_id, {:result, cur_val})
- {:add, val1, val2} -> cur_val + val1 + val2
- end
- loop(result)
- end
- def get_result(receiver_id) do
- send(receiver_id, {:value, self()})
- receive do
- {:result, result} -> result
- end
- end
- def add(receiver_id, val1, val2) do
- send(receiver_id, {:add, val1, val2})
- get_result(receiver_id)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement