Lesson 12 of 16

Enum

Enum

The Enum module provides functions for working with collections:

map

Enum.map applies a function to each element:

doubled = Enum.map([1, 2, 3, 4, 5], fn x -> x * 2 end)
IO.puts(Enum.join(doubled, ", "))   # 2, 4, 6, 8, 10

filter

Enum.filter keeps elements matching a condition:

evens = Enum.filter([1, 2, 3, 4, 5], fn x -> rem(x, 2) == 0 end)
IO.puts(Enum.join(evens, ", "))   # 2, 4

reduce

Enum.reduce folds a list into a single value:

total = Enum.reduce([1, 2, 3, 4, 5], 0, fn x, acc -> acc + x end)
IO.puts(total)   # 15

Your Turn

Given nums = [1, 2, 3, 4, 5]:

  1. Map to doubles → "2, 4, 6, 8, 10"
  2. Filter evens → "2, 4"
  3. Reduce to sum → 15
JS Transpiler loading...
Loading...
Click "Run" to execute your code.