-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_2.35.exs
47 lines (37 loc) · 1.23 KB
/
exercise_2.35.exs
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
38
39
40
41
42
43
44
45
46
47
defmodule Accumulate do
def accum(_op, init, []), do: init
def accum(op, init, [head | tail]), do: op.(head, accum(op, init, tail))
end
defmodule CountLeaves do
def of(list, :recurse), do: of_recurse(list)
def of(list, :accum) do
g = fn
l when is_list(l) -> of(l, :accum)
n -> 1
end
Accumulate.accum(&(&1 + &2), 0, Enum.map(list, g))
end
defp of_recurse([]), do: 0
defp of_recurse([head | tail]), do: of_recurse(head) + of_recurse(tail)
defp of_recurse(_), do: 1
end
ExUnit.start
defmodule CountLeavesTest do
use ExUnit.Case, async: true
test "it counts leaves in an empty list" do
assert CountLeaves.of([], :recurse) == 0
assert CountLeaves.of([], :accum) == 0
end
test "it count leaves in a flat list" do
assert CountLeaves.of([1,2,3], :recurse) == 3
assert CountLeaves.of([1,2,3], :accum) == 3
end
test "it count leaves in nested lists" do
assert CountLeaves.of([[1, 2], 3], :recurse) == 3
assert CountLeaves.of([1, [2, 3]], :recurse) == 3
assert CountLeaves.of([[1, 2], [3, 4]], :recurse) == 4
assert CountLeaves.of([[1, 2], 3], :accum) == 3
assert CountLeaves.of([1, [2, 3]], :accum) == 3
assert CountLeaves.of([[1, 2], [3, 4]], :accum) == 4
end
end