a simple elixir enum cheatsheet
-
all?[ππΌ π€πΌ ππΌ βπΌ ππΌ ππΌ] |> Enum.all?( fn x -> x == ππΌ end) ---> false
-
any?[ππΌ π€πΌ ππΌ βπΌ ππΌ ππΌ] |> Enum.any?( fn x -> x == ππΌ end) ---> true
-
at[π π π π π π ] |> Enum.at(5) ---> π
-
chunk_by[π π π π π ] |> Enum.chunk_by(fn x -> x == π) ---> [[π π] [π] [π π ]]
-
chunk_every[π π π π€² π€] |> Enum.chunk_every(2) ---> [[π π] [π π€²] [π€]]
-
concat[πΆ π± π] |> Enum.concat([πΉ π° π¦]) ---> [πΆ π± π πΉ π° π¦]
-
count[πΆ π± π πΉ π° π¦] |> Enum.count() ---> 6
-
dedup- remove consecutive duplicates[πΉ π° π¦ π¦ πΉ π°] |> Enum.dedup() ---> [πΉ π° π¦ πΉ π°]
-
drop[ππΌ π€πΌ ππΌ π¦ πΉ π°] |> Enum.drop(3) ---> [π¦ πΉ π°]
-
drop_every1..10 |> Enum.drop_every(2) ---> [2, 4, 6, 8, 10] 1..10 |> Enum.drop_every(1) ---> [] 1..10 |> Enum.drop_every(0) ---> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-
drop_while1..10 |> Enum.drop_while(fn x -> x < 5 end) ---> [6, 7, 8, 9, 10]
-
each1..2 |> Enum.each(fn x -> IO.puts(x) end) ---> 1 \n 2
-
empty?[] |> Enum.empty?() ---> true
-
fetch[ππΌ π€πΌ ππΌ π¦ πΉ π°] |> Enum.fetch(3) ---> {:ok, π¦}
-
filter1..10 |> Enum.filter(fn x -> rem(x, 2) == 0 end) --> [2, 4, 6, 8, 10]
-
find[πΉ π° π¦ π¦ πΉ π°] |> Enum.find(fn x -> x == π¦ end) ---> π¦
-
find_index[πΉ π° π¦ π¦ πΉ π°] |> Enum.find(fn x -> x == π¦ end) ---> 2
-
flat_map["πΉ", "π¦"] |> Enum.flat_map(fn x -> [x, x] end) ---> [["πΉ", "πΉ"], ["π¦", "π¦"]]
-
frequencies["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.frequencies() ---> %{"π°" => 1, "πΉ" => 2, "π¦" => 2}
-
frequencies_by["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.frequencies_by(fn x -> x == "π¦" end) ---> %{false: 3, true: 2}
-
group_by["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.group_by(fn x -> x end) ---> %{"πΉ" => ["πΉ","πΉ"], "π°" => ["π°"], "π¦" => ["π¦","π¦"]}
["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.group_by(fn x -> x end, fn x -> name_of(x) end) ---> %{"πΉ" => ["bear","bear"], "π°" => ["rabbit"], "π¦" => ["fox","fox"]}
-
intersperse["πΉ", "π¦", "π¦", "πΉ"] |> Enum.intersperse("π°") ---> ["πΉ", "π°", "π¦", "π°", "π¦", "π°", "πΉ"] ["πΉ"] |> Enum.intersperse("π°") ---> ["πΉ"] [] |> Enum.intersperse("π°") ---> []
-
into["πΉ", "π¦", "π¦", "πΉ"] |> Enum.into([]) ---> ["πΉ", "π¦", "π¦", "πΉ"] %{bear: "πΉ", fox: "π¦"} |> Enum.into([]) ---> [bear: "πΉ", fox: "π¦"] %{bear: "π°", fox: "π¦", bear: "πΉ"} |> Enum.into([]) ---> [bear: "πΉ", fox: "π¦"]
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.into([], fn x -> name_of(x) end) ---> ["bear", "fox", "fox", "bear"]
-
join["πΉ", "π¦", "π¦", "πΉ"] |> Enum.join("π°") ---> "πΉπ°π¦π°π¦π°πΉ"
-
map["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map(fn x -> "#{x}π°" end) ---> ["πΉπ°", "π¦π°", "π¦π°", "πΉπ°"]
-
map_every["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map_every(2, fn x -> "#{x}π°" end) ---> ["πΉ", "π¦π°", "π¦", "πΉπ°"]
-
map_intersperse["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map_intersperse("πΆ", fn x -> "#{x}π°" end) ---> ["πΉπ°", "πΆπ°", "π¦π°", "πΆπ°", "π¦π°", "πΆπ°", "πΉπ°"]
-
map_join["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map_join("πΆ", fn x -> "#{x}π°" end) ---> "πΉπ°πΆπ¦π°πΆπ¦π°πΆπΉπ°"
-
map_reduce["πΉ", "π¦", "π¦", "π°"] |> Enum.map_reduce("πΆ", fn x, acc -> {"#{x}π°", "#{x}#{acc}"} end) ---> {["πΉπ°", "π¦π°", "π¦π°", "π°π°"], "π°π¦π¦πΉπΆ" }
-
max[1, 3, 6, 9] |> Enum.max() ---> 9
-
max_by["πΉ", "π¦", "π¦", "π°"] |> Enum.max_by(fn x -> name_of(x) end) ---> "π°"
-
member?["πΉ", "π¦", "π¦", "π°"] |> Enum.member?("π°") ---> true ["πΉ", "π¦", "π¦" |> Enum.member?("π°") ---> false
-
min[1, 3, 6, 9] |> Enum.min() ---> 1
-
min_by["πΉ", "π¦", "π¦", "π°"] |> Enum.min_by(fn x -> name_of(x) end) ---> "πΉ"
-
min_max[1, 2, 3, 4, 5, 9] |> Enum.min_max() ---> {1, 9}
-
min_max_by["πΉ", "π¦", "π¦", "π°"] |> Enum.min_max_by(fn x -> name_of(x) end) ---> {"πΉ", "π°"}
-
random["πΉ", "π¦", "π¦", "π°"] |> Enum.random() ----> "π¦"
-
reduce1..10 |> Enum.reduce(0, fn (x, agg) -> agg + x end) ----> 55
-
reduce_while[] |> Enum.reduce_while()
-
reject[] |> Enum.reject()
-
reverse["πΉ", "π¦", "π°"] |> Enum.reverse() -----> ["π°", "π¦","πΉ"]
-
reverse_slice[] |> Enum.reverse_slice()
-
scan[] |> Enum.scan()
-
shuffle["πΉ", "π¦", "π°"] |> Enum.shuffle() ------> ["πΉ", "π°", "π¦"]
-
slice["πΉ", "π¦", "π°"] |> Enum.slice(1,2) -----> ["π¦", "π°"]
-
sort[45, 36, 78, 1, 4] |> Enum.sort() -------> [1, 4, 36, 45, 78]
-
sort_by[] |> Enum.sort_by()
-
split["πΉ", "π¦", "π°"] |> Enum.split(2) -----> {["πΉ", "π¦"], ["π°"]}
-
split_while[] |> Enum.split_while()
-
split_with[] |> Enum.split_with()
-
sum[1, 2, 3, 4, 5] |> Enum.sum() -----> 15
product
[1, 2, 3, 4, 5] |> Enum.product() -----> 120
-
take[] |> Enum.take()
-
take_every[] |> Enum.take_every()
-
take_random[] |> Enum.take_random()
-
take_while[] |> Enum.take_while()
-
to_list[] |> Enum.to_list()
-
uniq["πΉ", "π¦", "π¦","π°", "π°"] |> Enum.uniq() -----> ["πΉ", "π¦", "π°"]
-
uniq_by[] |> Enum.uniq_by()
-
unzip[] |> Enum.unzip()
-
with_index["πΉ", "π¦", "π°"] |> Enum.with_index() -----> [{"πΉ", 0}, {"π¦", 1}, {"π°", 2}]
-
zip[] |> Enum.zip()