Elixir – Enum.reduce & Enum.map

Elixir provides Enum that provides a set of algorithms that enumerate over enumerables according to the Enumerable protocol. It have much functions you can use for processing your enumerable data. Two of them is Enum.map and Enum.reduce that we use to processing data.

Enum.map

It use when you want to get processing each item on your list and return set of list on it. For example:

data = [1,2,3,4]
data_square = Enum.map(data, fn(x) -> x*x end)

data_square output:

[1, 4, 9, 16]

they return data on list too.

Enum.map will doing recursion on list data and return data in list where the value from list grab from the process that run in every enumaerate process.

Enum.reduce

When we using reduce it output will on single value because the proccess will accumulate data from the process. For example (use previous data):

square_total = Enum.reduce(data, fn(x,acc) -> x*x + acc end)

output is : 30

Reduce will processing every data in the list. They have an accumulator (we define in “acc” value) to save total value from last process.