Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

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()

```