Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Everything is Reduce

Everything is Reduce

A presentation about the `Enum.reduce/3` function in Elixir.

Presented at:
- Elixir Girls workshop on 28 July 2017
- Sydney Elixir Meetup on 2 August 2017
- Elixir Camp 4 on 27 October 2018
- Culture Amp brownbag lunch meetup on 3 July 2019

Presentation slide deck markdown and speaker notes (useable in Deckset): https://github.com/paulfioravanti/presentations/tree/master/everything_is_reduce

Paul Fioravanti

July 27, 2017
Tweet

More Decks by Paul Fioravanti

Other Decks in Programming

Transcript

  1. def pulp([ ! , , ]) do " end 9/75

    — @paulfioravanti
  2. Enum.each([1, 2, 3], fn number -> IO.puts("Current number is #{number}")

    end) Current number is 1 Current number is 2 Current number is 3 #=> :ok 26/75 — @paulfioravanti
  3. Enum.map([1, 2, 3], fn number -> number * 2 end)

    #=> 30/75 — @paulfioravanti
  4. Enum.map([1, 2, 3], fn number -> number * 2 end)

    #=> [2, 4, 6] 31/75 — @paulfioravanti
  5. Requirements → Take in a list → Return a map

    → Loop over each element in list → Add letter key to map if it doesn't exist → If letter key exists, increment its value 37/75 — @paulfioravanti
  6. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, acc,

    fun) #=> %{"a" => 3, "b" => 1, "c" => 2} 46/75 — @paulfioravanti
  7. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fun) #=> %{"a" => 3, "b" => 1, "c" => 2} 47/75 — @paulfioravanti
  8. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{"a"

    => 10}, fun) #=> %{"a" => 3, "b" => 1, "c" => 2} 48/75 — @paulfioravanti
  9. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fun) #=> %{"a" => 3, "b" => 1, "c" => 2} 49/75 — @paulfioravanti
  10. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fn char, map -> # ... # ... # ... end) #=> %{"a" => 3, "b" => 1, "c" => 2} 50/75 — @paulfioravanti
  11. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fn char, map -> Map.update(map, char, 1, fn value -> value + 1 end) end) #=> %{"a" => 3, "b" => 1, "c" => 2} 51/75 — @paulfioravanti
  12. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fn char, map -> Map.update(map, char, 1, fn value -> value + 1 end) end) #=> %{"a" => 3, "b" => 1, "c" => 2} 52/75 — @paulfioravanti
  13. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fn char, map -> Map.update(map, char, 1, fn value -> value + 1 end) end) #=> %{"a" => 3, "b" => 1, "c" => 2} 53/75 — @paulfioravanti
  14. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fn char, map -> Map.update(map, char, 1, fn value -> value + 1 end) end) #=> %{"a" => 3, "b" => 1, "c" => 2} 54/75 — @paulfioravanti
  15. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fn char, map -> Map.update(map, char, 1, fn value -> value + 1 end) end) #=> %{"a" => 3, "b" => 1, "c" => 2} 55/75 — @paulfioravanti
  16. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, %{},

    fn char, map -> Map.update(map, char, 1, fn value -> value + 1 end) end) #=> %{"a" => 3, "b" => 1, "c" => 2} 56/75 — @paulfioravanti
  17. Enum.map([1, 2, 3], fn number -> number * 2 end)

    #=> [2, 4, 6] 58/75 — @paulfioravanti
  18. Enum.reduce([1, 2, 3], [], fn number, list -> [number *

    2 | list] end) #=> 59/75 — @paulfioravanti
  19. Enum.reduce([1, 2, 3], [], fn number, list -> [number *

    2 | list] end) #=> [6, 4, 2] 60/75 — @paulfioravanti
  20. Enum.reduce([1, 2, 3], [], fn number, list -> [number *

    2 | list] end) |> Enum.reverse() #=> [2, 4, 6] 61/75 — @paulfioravanti
  21. Enum.map([1, 2, 3], fn number -> number * 2 end)

    #=> [2, 4, 6] Enum.reduce([1, 2, 3], [], fn number, list -> [number * 2 | list] end) |> Enum.reverse() #=> [2, 4, 6] 62/75 — @paulfioravanti
  22. Enum.each([1, 2, 3], fn number -> IO.puts("Current number is #{number}")

    end) Current number is 1 Current number is 2 Current number is 3 #=> :ok 64/75 — @paulfioravanti
  23. Enum.reduce([1, 2, 3], nil, fn number, _acc -> IO.puts("Current number

    is #{number}") end) Current number is 1 Current number is 2 Current number is 3 #=> :ok 65/75 — @paulfioravanti
  24. Enum.reduce([1, 2, 3], nil, fn number, _acc -> IO.puts("Current number

    is #{number}") end) Current number is 1 Current number is 2 Current number is 3 #=> :ok 66/75 — @paulfioravanti
  25. Enum.reduce([1, 2, 3], nil, fn number, _acc -> IO.puts("Current number

    is #{number}") end) Current number is 1 Current number is 2 Current number is 3 #=> :ok 67/75 — @paulfioravanti
  26. Enum.reduce([1, 2, 3], nil, fn number, _acc -> IO.puts("Current number

    is #{number}") end) Current number is 1 Current number is 2 Current number is 3 #=> :ok 68/75 — @paulfioravanti
  27. Enum.each([1, 2, 3], fn number -> IO.puts("Current number is #{number}")

    end) Current number is 1 Current number is 2 Current number is 3 #=> :ok Enum.reduce([1, 2, 3], nil, fn number, _acc -> IO.puts("Current number is #{number}") end) Current number is 1 Current number is 2 Current number is 3 #=> :ok 69/75 — @paulfioravanti