-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.ex
37 lines (26 loc) · 809 Bytes
/
node.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
defmodule Client do
def receive do
receive do
{function, args, caller} ->
IO.puts "#{Node.self} received a function and #{length(args)} args"
start = DateTime.to_unix(DateTime.utc_now)
# Execute "function" for each arg in "args", and put all "arg: result" tuples in a map
results = Enum.reduce args, %{}, fn arg, acc -> Map.put(acc, arg, function.(arg)) end
duration = DateTime.to_unix(DateTime.utc_now) - start
IO.puts "#{Node.self} took #{duration} seconds to execute"
# Respond to the caller with the results map
send caller, results
end
end
end
defmodule Maths do
def fibonacci 0 do
1
end
def fibonacci 1 do
1
end
def fibonacci(n) when n > 0 do
fibonacci(n - 1) + fibonacci(n - 2)
end
end