This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = Enum.to_list(1..10_000) | |
map_fun = fn(i) -> [i, i * i] end | |
Benchee.run(%{ | |
"flat_map" => fn -> Enum.flat_map(list, map_fun) end, | |
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end | |
}, | |
formatters: [ | |
&Benchee.Formatters.HTML.output/1, | |
&Benchee.Formatters.Console.output/1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DeepMerge.deep_merge(%{a: 1, b: [x: 10, y: 9]}, %{b: [y: 20, z: 30], c: 4}) | |
# => %{a: 1, b: [x: 10, y: 20, z: 30], c: 4} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = Enum.to_list(1..10_000) | |
map_fun = fn(i) -> [i, i * i] end | |
Benchee.run(%{time: 3, console: %{unit_scaling: :smallest}}, %{ | |
"flat_map" => fn -> Enum.flat_map(list, map_fun) end, | |
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = Enum.to_list(1..10_000) | |
map_fun = fn(i) -> [i, i * i] end | |
Benchee.run(%{time: 3}, %{ | |
"flat_map" => fn -> Enum.flat_map(list, map_fun) end, | |
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = Enum.to_list(1..10_000) | |
map_fun = fn(i) -> [i, i * i] end | |
Benchee.run(%{time: 3, parallel: 2}, %{ | |
"flat_map" => fn -> Enum.flat_map(list, map_fun) end, | |
"map.flatten" => fn -> list |> Enum.map(map_fun) |> List.flatten end | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = Enum.to_list(1..10_000) | |
map_fun = fn(i) -> [i, i * i] end | |
Benchee.run( | |
%{ | |
formatters: [ | |
&Benchee.Formatters.CSV.output/1, | |
&Benchee.Formatters.Console.output/1 | |
], | |
csv: %{file: "my.csv"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Number do | |
def is_even?(0), do: true | |
def is_even?(n) do | |
!is_even?(n - 1) | |
end | |
def is_even_tco?(n, acc \\ true) | |
def is_even_tco?(0, acc), do: acc | |
def is_even_tco?(n, acc) do | |
is_even_tco?(n - 1, !acc) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = Enum.to_list(1..100_000_000) | |
MyMap.map_body(list, fn(i) -> i + 1 end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list = Enum.to_list(1..10_000) | |
map_fun = fn(i) -> i + 1 end | |
Benchee.run %{ | |
"map tail-recursive with ++" => | |
fn -> MyMap.map_tco_concat(list, map_fun) end, | |
"map with TCO reverse" => | |
fn -> MyMap.map_tco(list, map_fun) end, | |
"stdlib map" => | |
fn -> Enum.map(list, map_fun) end, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyMap do | |
def map_tco(list, function) do | |
Enum.reverse _map_tco([], list, function) | |
end | |
defp _map_tco(acc, [head | tail], function) do | |
_map_tco([function.(head) | acc], tail, function) | |
end | |
defp _map_tco(acc, [], _function) do |