Created
July 12, 2015 20:16
-
-
Save zsiciarz/558ef77be1e4f1bac5dd to your computer and use it in GitHub Desktop.
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 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