Skip to content

Instantly share code, notes, and snippets.

@javiervidal
Last active March 31, 2017 12:30
Show Gist options
  • Save javiervidal/83e24bb12ceeefd0fa3501c3c55dd01c to your computer and use it in GitHub Desktop.
Save javiervidal/83e24bb12ceeefd0fa3501c3c55dd01c to your computer and use it in GitHub Desktop.
Lists and Recursion
defmodule MyList do
def map([], _func), do: []
def map([ head | tail ], func), do: [ func.(head) | map(tail, func) ]
end
MyList.map [1,2,3,4], fn (n) -> n*n end
MyList.map [1,2,3,4], &(&1 * &1)
defmodule MyList do
def reduce([], value, _) do
value
end
def reduce([head | tail], value, func) do
reduce(tail, func.(head, value), func)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment