Collections

Functional so no inplace

Provies set of algs to enumerate enumeratable types

Loop, length

Enum.each list4, fn item ->
	IO.puts item
end
len = Enum.count(l)

Map, Reduce, Filter

Enum.map([1, 2, 3], fn x -> x * 2 end)
Enum.map(%{1 => 2, 3 => 4}, fn {k, v} -> k * v end)
#[2, 12]
Enum.map(%{1 => 2, 3 => 4}, fn {k, _} -> k === 3 end) #[false, true]
%{monday: 28, tuesday: 29, wednesday: 29}
|> Enum.map(fn ({d, t}) -> {d, t * 1.8 + 32} end)
|> Enum.into(%{})

Enum.reduce(1..3, 0, fn (new, acc) -> new + acc end)
Enum.reduce([%{a: 1}, %{b: 2}], %{}, &Map.merge/2)

Enum.filter([1,2,3], fn a -> a in [1,3] end) #[1,3]
Enum.filter([1,2,3], fn a -> a not in [1,3] end)
Enum.filter(%{a: 1, b: 4}, fn {x, y} -> y === 1 end)
#[a: 1]

#returns first val that fn returns truthy val or default
Enum.find(enum, default, fn)
Enum.find([1,2], 3, fn x -> x > 3 end) # 3

Enums are eager so a pipe means an intermediate list is always created

Other

  • .shuffle(enum)

  • .member(enum, el)

  • .split(enum, count) => returns tuple of two enums with count in first one

Sort

Sorting Dates

Do not use <= or >= when using structs/dates

Passing a module, uses that module's compare/2

Comprehension

Parallelize

Tasks

Streams

Useful for large collections

Streams are lazy build series of computations that only becomes something when passed to Enum

Stream also has fts to create streams

Last updated