Skip to content

mudssrali/elixir-enum-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

11 Commits
Β 
Β 

Repository files navigation

Elixir Enum Cheatsheet

a simple elixir enum cheatsheet

Functions

  • 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_every

    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

    1..10 |> Enum.drop_while(fn x -> x < 5 end) ---> [6, 7, 8, 9, 10]
  • each

    1..2 |> Enum.each(fn x -> IO.puts(x) end) ---> 1 \n 2
  • empty?

    [] |> Enum.empty?() ---> true
  • fetch

    [πŸ‘‹πŸΌ 🀚🏼 πŸ–πŸΌ 🦊 🐹 🐰] |> Enum.fetch(3) ---> {:ok, 🦊}
  • filter

    1..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() ----> "🦊"
  • reduce

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