-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathfilter_map.exs
68 lines (61 loc) · 1.51 KB
/
filter_map.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
defmodule FilterMap.For do
def filter_map(list, filter_fun, map_fun) do
for num <- list, filter_fun.(num), do: map_fun.(num)
end
end
defmodule FilterMap.FilterMap do
def filter_map(list, filter_fun, map_fun) do
list
|> Enum.filter(filter_fun)
|> Enum.map(map_fun)
end
end
defmodule FilterMap.FlatMap do
def filter_map(list, filter_fun, map_fun) do
Enum.flat_map(list, fn num ->
if filter_fun.(num) do
[map_fun.(num)]
else
[]
end
end)
end
end
defmodule FilterMap.ReduceReverse do
def filter_map(list, filter_fun, map_fun) do
list
|> Enum.reduce([], fn num, acc ->
if filter_fun.(num) do
[map_fun.(num) | acc]
else
acc
end
end)
|> Enum.reverse()
end
end
defmodule FilterMap.Benchmark do
@inputs %{
"Large" => 1..1_000_000,
"Medium" => 1..10000,
"Small" => 1..100
}
def benchmark do
Benchee.run(
%{
"for comprehension" => fn range -> bench_func(FilterMap.For, range) end,
"filter |> map" => fn range -> bench_func(FilterMap.FilterMap, range) end,
"flat_map" => fn range -> bench_func(FilterMap.FlatMap, range) end,
"reduce |> reverse" => fn range -> bench_func(FilterMap.ReduceReverse, range) end
},
time: 10,
memory_time: 0.01,
inputs: @inputs,
print: [fast_warning: false]
)
end
def bench_func(module, range) do
module.filter_map(range, &(rem(&1, 3) == 0), &(&1 + 1))
end
end
FilterMap.Benchmark.benchmark()