summaryrefslogtreecommitdiff
path: root/src/code/2/my_map_red.exs
blob: 51f47f8f210c243a30c46bc4567abe98d32180cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
defmodule MapReduce do
  def reduce([], acc, _), do: acc
  def reduce([h|t], acc, f) do
    reduce(t, f.(h, acc), f)
  end

  def map([], _), do: []
  def map(l, f) do
    reduce(l, [], fn(x, acc) -> acc ++ [f.(x)] end)
  end
end

[1, 2, 3, 4, 5]
|> MapReduce.map(fn(x) -> x * 2 end)
|> IO.inspect
|> MapReduce.reduce(0, fn(x, acc) -> acc + x end)
|> IO.inspect