The Enum Module
Enum works on anything implementing the Enumerable protocol: lists, maps, ranges, MapSets, streams.
Enum works on anything implementing the Enumerable protocol: lists, maps, ranges, MapSets, streams. It is eager.
Working with maps in Enum yields {key, value} tuples:
Enum.map([1, 2, 3], &(&1 * 2)) # [2, 4, 6]
Enum.filter(1..10, &(rem(&1, 2) == 0)) # [2, 4, 6, 8, 10]
Enum.reject(1..5, &(&1 > 3)) # [1, 2, 3]
Enum.reduce([1, 2, 3], 0, &+/2) # 6
Enum.reduce([1, 2, 3], fn x, acc -> x * acc end) # 6
Enum.sum([1, 2, 3]), Enum.product([2, 3])
Enum.count([1, 2, 3]), Enum.count(1..10, &(&1 > 5))
Enum.min([3, 1]), Enum.max([3, 1]), Enum.min_max([3, 1, 5])
Enum.min_by(users, & &1.age), Enum.max_by(users, & &1.age)
Enum.sort([3, 1, 2]), Enum.sort([3, 1, 2], :desc)
Enum.sort_by(users, & &1.name), Enum.sort_by(users, & &1.age, :desc)
Enum.sort(dates, Date) # sort with a module's compare/2
Enum.reverse([1, 2, 3])
Enum.uniq([1, 1, 2]), Enum.uniq_by(users, & &1.email)
Enum.find([1, 2, 3], &(&1 > 1)) # 2
Enum.find_index, Enum.find_value
Enum.any?([1, 2], &(&1 > 1)), Enum.all?([1, 2], &is_integer/1)
Enum.member?([1, 2], 2), Enum.empty?([])
Enum.at([1, 2, 3], 1), Enum.fetch([1], 5)
Enum.take([1, 2, 3], 2), Enum.drop([1, 2, 3], 1)
Enum.take_while, Enum.drop_while, Enum.split, Enum.split_with
Enum.chunk_every([1, 2, 3, 4, 5], 2) # [[1, 2], [3, 4], [5]]
Enu
…%{a: 1, b: 2}
|> Enum.map(fn {k, v} -> {k, v * 10} end)
|> Map.new()