Skip to content

Commit 455d345

Browse files
author
Bernard Pietraga
committed
Add exercises 6 and 7 from chapter 14
1 parent c602fed commit 455d345

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# self is a function and assigned points to parent
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
defmodule Parallel do
2+
def pmap(collection, fun) do
3+
me = self()
4+
collection
5+
|> Enum.map(fn (elem) ->
6+
spawn_link fn -> (send me, {self(), fun.(elem)}) end
7+
end)
8+
|> Enum.reverse
9+
|> Enum.reverse
10+
|> Enum.map(fn (pid) ->
11+
receive do {_pid, result} -> result end
12+
end)
13+
end
14+
end
15+
16+
ExUnit.start
17+
18+
defmodule ParallelTest do
19+
use ExUnit.Case
20+
21+
test "pmap returns list with proper sorted order" do
22+
square_list = [
23+
1, 4, 9, 16, 25, 36, 49, 64, 81,
24+
100, 121, 144, 169, 196, 225, 256,
25+
289, 324, 361, 400, 441, 484, 529,
26+
576, 625, 676, 729, 784, 841, 900,
27+
961, 1024, 1089, 1156, 1225, 1296,
28+
1369, 1444, 1521, 1600, 1681, 1764,
29+
1849, 1936, 2025, 2116, 2209, 2304,
30+
2401, 2500
31+
]
32+
33+
expected = Parallel.pmap(1..50, &(&1 * &1))
34+
assert expected != square_list
35+
end
36+
end

0 commit comments

Comments
 (0)