Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. defmodule Map do
  2. def map([], _), do: []
  3. def map([head|tail], fun), do: [fun.(head) | map(tail, fun)]
  4.  
  5. def reduce([], acumulator, _), do: acumulator
  6. def reduce([head|tail], acumulator, fun), do: reduce(tail, fun.(head, acumulator), fun)
  7.  
  8. def mapsum(list, fun) do
  9. map(list, fun)
  10. |> reduce(0, &(&1 + &2))
  11. end
  12.  
  13. def max([head|tail]) do
  14. compare = fn
  15. a,b when a > b -> a
  16. a,b when b >= a -> b
  17. end
  18.  
  19. reduce([head|tail], head, compare)
  20. end
  21. end
  22.  
  23. list = [1,2,3]
  24. squares = Map.map(list, &(&1 * &1))
  25. IO.puts Map.mapsum(squares, &(&1 + 1))
  26. IO.puts Map.max(list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement