Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- defmodule Map do
- def map([], _), do: []
- def map([head|tail], fun), do: [fun.(head) | map(tail, fun)]
- def reduce([], acumulator, _), do: acumulator
- def reduce([head|tail], acumulator, fun), do: reduce(tail, fun.(head, acumulator), fun)
- def mapsum(list, fun) do
- map(list, fun)
- |> reduce(0, &(&1 + &2))
- end
- def max([head|tail]) do
- compare = fn
- a,b when a > b -> a
- a,b when b >= a -> b
- end
- reduce([head|tail], head, compare)
- end
- end
- list = [1,2,3]
- squares = Map.map(list, &(&1 * &1))
- IO.puts Map.mapsum(squares, &(&1 + 1))
- IO.puts Map.max(list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement