Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mudssrali/elixir-enum-cheatsheet
a simple elixir enum cheatsheet with emoji
https://github.com/mudssrali/elixir-enum-cheatsheet
cheatsheet education elixir elixir-lang enum learning-elixir
Last synced: 3 days ago
JSON representation
a simple elixir enum cheatsheet with emoji
- Host: GitHub
- URL: https://github.com/mudssrali/elixir-enum-cheatsheet
- Owner: mudssrali
- Created: 2021-04-11T17:36:19.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-23T11:42:53.000Z (almost 3 years ago)
- Last Synced: 2023-03-06T08:17:09.420Z (over 1 year ago)
- Topics: cheatsheet, education, elixir, elixir-lang, enum, learning-elixir
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Elixir Enum Cheatsheet
a simple elixir enum cheatsheet
## Functions
- `all?`
```ex
[ππΌ π€πΌ ππΌ βπΌ ππΌ ππΌ] |> Enum.all?( fn x -> x == ππΌ end) ---> false
```
- `any?`
```ex
[ππΌ π€πΌ ππΌ βπΌ ππΌ ππΌ] |> Enum.any?( fn x -> x == ππΌ end) ---> true
```
- `at`
```ex
[π π π π π π ] |> Enum.at(5) ---> π
```
- `chunk_by`
```ex
[π π π π π ] |> Enum.chunk_by(fn x -> x == π) ---> [[π π] [π] [π π ]]
```
- `chunk_every`
```ex
[π π π π€² π€] |> Enum.chunk_every(2) ---> [[π π] [π π€²] [π€]]
```
- `concat`
```ex
[πΆ π± π] |> Enum.concat([πΉ π° π¦]) ---> [πΆ π± π πΉ π° π¦]
```
- `count`
```ex
[πΆ π± π πΉ π° π¦] |> Enum.count() ---> 6
```
- `dedup` - remove consecutive duplicates
```ex
[πΉ π° π¦ π¦ πΉ π°] |> Enum.dedup() ---> [πΉ π° π¦ πΉ π°]
```
- `drop`
```ex
[ππΌ π€πΌ ππΌ π¦ πΉ π°] |> Enum.drop(3) ---> [π¦ πΉ π°]
```
- `drop_every`
```ex
1..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_while`
```ex
1..10 |> Enum.drop_while(fn x -> x < 5 end) ---> [6, 7, 8, 9, 10]
```
- `each`
```ex
1..2 |> Enum.each(fn x -> IO.puts(x) end) ---> 1 \n 2
```
- `empty?`
```ex
[] |> Enum.empty?() ---> true
```
- `fetch`
```ex
[ππΌ π€πΌ ππΌ π¦ πΉ π°] |> Enum.fetch(3) ---> {:ok, π¦}
```
- `filter`
```ex
1..10 |> Enum.filter(fn x -> rem(x, 2) == 0 end) --> [2, 4, 6, 8, 10]
```
- `find`
```ex
[πΉ π° π¦ π¦ πΉ π°] |> Enum.find(fn x -> x == π¦ end) ---> π¦
```
- `find_index`
```ex
[πΉ π° π¦ π¦ πΉ π°] |> Enum.find(fn x -> x == π¦ end) ---> 2
```
- `flat_map`
```ex
["πΉ", "π¦"] |> Enum.flat_map(fn x -> [x, x] end) ---> [["πΉ", "πΉ"], ["π¦", "π¦"]]
```
- `frequencies`
```ex
["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.frequencies() ---> %{"π°" => 1, "πΉ" => 2, "π¦" => 2}
```
- `frequencies_by`
```ex
["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.frequencies_by(fn x -> x == "π¦" end) ---> %{false: 3, true: 2}```
- `group_by`
```ex
["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.group_by(fn x -> x end) ---> %{"πΉ" => ["πΉ","πΉ"], "π°" => ["π°"], "π¦" => ["π¦","π¦"]}
``````ex
["πΉ", "π°", "π¦", "π¦", "πΉ"] |> Enum.group_by(fn x -> x end, fn x -> name_of(x) end) ---> %{"πΉ" => ["bear","bear"], "π°" => ["rabbit"], "π¦" => ["fox","fox"]}```
- `intersperse`
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.intersperse("π°") ---> ["πΉ", "π°", "π¦", "π°", "π¦", "π°", "πΉ"]
["πΉ"] |> Enum.intersperse("π°") ---> ["πΉ"]
[] |> Enum.intersperse("π°") ---> []
```
- `into`
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.into([]) ---> ["πΉ", "π¦", "π¦", "πΉ"]
%{bear: "πΉ", fox: "π¦"} |> Enum.into([]) ---> [bear: "πΉ", fox: "π¦"]
%{bear: "π°", fox: "π¦", bear: "πΉ"} |> Enum.into([]) ---> [bear: "πΉ", fox: "π¦"]
```
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.into([], fn x -> name_of(x) end) ---> ["bear", "fox", "fox", "bear"]
```
- `join`
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.join("π°") ---> "πΉπ°π¦π°π¦π°πΉ"
```
- `map`
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map(fn x -> "#{x}π°" end) ---> ["πΉπ°", "π¦π°", "π¦π°", "πΉπ°"]
```
- `map_every`
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map_every(2, fn x -> "#{x}π°" end) ---> ["πΉ", "π¦π°", "π¦", "πΉπ°"]
```
- `map_intersperse`
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map_intersperse("πΆ", fn x -> "#{x}π°" end) ---> ["πΉπ°", "πΆπ°", "π¦π°", "πΆπ°", "π¦π°", "πΆπ°", "πΉπ°"]
```
- `map_join`
```ex
["πΉ", "π¦", "π¦", "πΉ"] |> Enum.map_join("πΆ", fn x -> "#{x}π°" end) ---> "πΉπ°πΆπ¦π°πΆπ¦π°πΆπΉπ°"
```
- `map_reduce`
```ex
["πΉ", "π¦", "π¦", "π°"] |> Enum.map_reduce("πΆ", fn x, acc -> {"#{x}π°", "#{x}#{acc}"} end) ---> {["πΉπ°", "π¦π°", "π¦π°", "π°π°"], "π°π¦π¦πΉπΆ" }
```
- `max`
```ex
[1, 3, 6, 9] |> Enum.max() ---> 9
```
- `max_by`
```ex
["πΉ", "π¦", "π¦", "π°"] |> Enum.max_by(fn x -> name_of(x) end) ---> "π°"
```
- `member?`
```ex
["πΉ", "π¦", "π¦", "π°"] |> Enum.member?("π°") ---> true
["πΉ", "π¦", "π¦" |> Enum.member?("π°") ---> false
```
- `min`
```ex
[1, 3, 6, 9] |> Enum.min() ---> 1
```
- `min_by`
```ex
["πΉ", "π¦", "π¦", "π°"] |> Enum.min_by(fn x -> name_of(x) end) ---> "πΉ"
```
- `min_max`
```ex
[1, 2, 3, 4, 5, 9] |> Enum.min_max() ---> {1, 9}
```
- `min_max_by`
```ex
["πΉ", "π¦", "π¦", "π°"] |> Enum.min_max_by(fn x -> name_of(x) end) ---> {"πΉ", "π°"}
```
- `random`
```ex
["πΉ", "π¦", "π¦", "π°"] |> Enum.random() ----> "π¦"
```
- `reduce`
```ex
1..10 |> Enum.reduce(0, fn (x, agg) -> agg + x end) ----> 55
```
- `reduce_while`
```ex
[] |> Enum.reduce_while()
```
- `reject`
```ex
[] |> Enum.reject()
```
- `reverse`
```ex
["πΉ", "π¦", "π°"] |> Enum.reverse() -----> ["π°", "π¦","πΉ"]
```
- `reverse_slice`
```ex
[] |> Enum.reverse_slice()
```
- `scan`
```ex
[] |> Enum.scan()
```
- `shuffle`
```ex
["πΉ", "π¦", "π°"] |> Enum.shuffle() ------> ["πΉ", "π°", "π¦"]
```
- `slice`
```ex
["πΉ", "π¦", "π°"] |> Enum.slice(1,2) -----> ["π¦", "π°"]
```
- `sort`
```ex
[45, 36, 78, 1, 4] |> Enum.sort() -------> [1, 4, 36, 45, 78]
```
- `sort_by`
```ex
[] |> Enum.sort_by()
```
- `split`
```ex
["πΉ", "π¦", "π°"] |> Enum.split(2) -----> {["πΉ", "π¦"], ["π°"]}
```
- `split_while`
```ex
[] |> Enum.split_while()
```
- `split_with`
```ex
[] |> Enum.split_with()
```
- `sum`
```ex
[1, 2, 3, 4, 5] |> Enum.sum() -----> 15
```
- `product`
```ex
[1, 2, 3, 4, 5] |> Enum.product() -----> 120
```
- `take`
```ex
[] |> Enum.take()
```
- `take_every`
```ex
[] |> Enum.take_every()
```
- `take_random`
```ex
[] |> Enum.take_random()
```
- `take_while`
```ex
[] |> Enum.take_while()
```
- `to_list`
```ex
[] |> Enum.to_list()
```
- `uniq`
```ex
["πΉ", "π¦", "π¦","π°", "π°"] |> Enum.uniq() -----> ["πΉ", "π¦", "π°"]
```
- `uniq_by`
```ex
[] |> Enum.uniq_by()
```
- `unzip`
```ex
[] |> Enum.unzip()
```
- `with_index`
```ex
["πΉ", "π¦", "π°"] |> Enum.with_index() -----> [{"πΉ", 0}, {"π¦", 1}, {"π°", 2}]
```
- `zip`
```ex
[] |> Enum.zip()
```