Skip to content

Instantly share code, notes, and snippets.

@DanielVartanov
Last active February 14, 2016 10:00
Show Gist options
  • Save DanielVartanov/b81feff66d070942722b to your computer and use it in GitHub Desktop.
Save DanielVartanov/b81feff66d070942722b to your computer and use it in GitHub Desktop.
defmodule MyEnum do
def reduce([], accumulator, _function) do
accumulator
end
def reduce([head | tail], accumulator, function) do
reduce tail, function.(accumulator, head), function
end
def map(list, function) do
reduce(list, [], fn(mapped_list, e) ->
[function.(e) | mapped_list]
end) |> Enum.reverse
end
end
IO.puts MyEnum.reduce([1, 2, 3, 4], 0, &+/2)
IO.puts MyEnum.reduce([1, 2, 3, 4], 1, &*/2)
IO.inspect MyEnum.map([1, 2, 3, 4], &(&1 * &1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment