Advertisement
PadmaJS

Untitled

Oct 10th, 2021
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.54 KB | None | 0 0
  1. defmodule Calculator do
  2. def start do
  3. spawn(fn -> loop(0) end)
  4. end
  5.  
  6. defp loop(cur_val) do
  7. result = receive do
  8. {:value, sender_id} -> send(sender_id, {:result, cur_val})
  9.  
  10. {:add, val1, val2} -> cur_val + val1 + val2
  11. end
  12. loop(result)
  13. end
  14.  
  15. def get_result(receiver_id) do
  16. send(receiver_id, {:value, self()})
  17. receive do
  18. {:result, result} -> result
  19. end
  20. end
  21.  
  22. def add(receiver_id, val1, val2) do
  23. send(receiver_id, {:add, val1, val2})
  24. get_result(receiver_id)
  25. end
  26. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement