Skip to content

Instantly share code, notes, and snippets.

@zsiciarz
Created July 12, 2015 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zsiciarz/558ef77be1e4f1bac5dd to your computer and use it in GitHub Desktop.
Save zsiciarz/558ef77be1e4f1bac5dd to your computer and use it in GitHub Desktop.
defmodule Functools do
def reduce(func, [head|tail], acc) do
reduce(func, tail, func.(head, acc))
end
def reduce(_func, [], acc) do
acc
end
def map(func, [head|tail]) do
[func.(head)|map(func, tail)]
end
def map(_func, []) do
[]
end
def filter(predicate, [head|tail]) do
if predicate.(head) do
[head|filter(predicate, tail)]
else
filter(predicate, tail)
end
end
def filter(_predicate, []) do
[]
end
end
IO.puts Functools.reduce(&+/2, [1, 2, 3], 0)
Functools.map(&IO.puts/1, [1, 2, 3])
IO.puts Functools.filter(fn x -> x != "d" end, ["d", "e", "f"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment