Last active
February 14, 2016 10:00
-
-
Save DanielVartanov/b81feff66d070942722b to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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