defmodule MyList do def map([], _func), do: [] def map([head|tail], func), do: [func.(head) | map(tail, func)] def reduce([], value, _func) do value end def reduce([head|tail], value, func) do reduce(tail, func.(head, value), func) end def mapsum(list, func) do map(list, func) |> reduce(0, &(&1 + &2)) end end
^o^ > iex practice_7_1.exs Eshell V8.0 (abort with ^G) Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help) iex(1)> MyList.mapsum([1, 2, 3], &(&1 * &1)) 14