Save 37% off PRO during our Black Friday Sale! »

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

Abda861707b1e78e0fce47ced55f84ee?s=128

Paul Fioravanti

July 27, 2017
Tweet

Transcript

  1. EVERYTHING IS reduce 1/75 — @paulfioravanti

  2. THIS PRESENTATION IS ABOUT 2/75 — @paulfioravanti

  3. THIS PRESENTATION IS ABOUT reduce 3/75 — @paulfioravanti

  4. TRANSFORM SOMETHING 4/75 — @paulfioravanti

  5. TRANSFORM TO A DIFFERENT FORM 5/75 — @paulfioravanti

  6. TRANSFORM TO A MORE BASIC FORM 6/75 — @paulfioravanti

  7. ! ➡ 7/75 — @paulfioravanti

  8. def pulp( ! ) do " end 8/75 — @paulfioravanti

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

    — @paulfioravanti
  10. def reduce([ ! , , ]) do " end 10/75

    — @paulfioravanti
  11. Enum 11/75 — @paulfioravanti

  12. Lists [1, 2, 3] 12/75 — @paulfioravanti

  13. Maps %{apple: " ! ", banana: " "} 13/75 —

    @paulfioravanti
  14. So... 14/75 — @paulfioravanti

  15. Enum.sum([1, 2, 3]) #=> 15/75 — @paulfioravanti

  16. Enum.sum([1, 2, 3]) #=> 6 16/75 — @paulfioravanti

  17. Enum.join(["Elixir", "is", "awesome!"], " ") #=> 17/75 — @paulfioravanti

  18. Enum.join(["Elixir", "is", "awesome!"], " ") #=> "Elixir is awesome!" 18/75

    — @paulfioravanti
  19. [1, 2, 3] 19/75 — @paulfioravanti

  20. PERFORM OPERATION 20/75 — @paulfioravanti

  21. DERIVE INFORMATION 21/75 — @paulfioravanti

  22. CREATE NEW INFORMATION 22/75 — @paulfioravanti

  23. ! 23/75 — @paulfioravanti

  24. [1, 2, 3] 24/75 — @paulfioravanti

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

    end) #=> 25/75 — @paulfioravanti
  26. 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
  27. [1, 2, 3] ⬇ :ok 27/75 — @paulfioravanti

  28. [1, 2, 3] ⬇ :ok 28/75 — @paulfioravanti

  29. [1, 2, 3] ⬇ [2, 4, 6] 29/75 — @paulfioravanti

  30. Enum.map([1, 2, 3], fn number -> number * 2 end)

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

    #=> [2, 4, 6] 31/75 — @paulfioravanti
  32. ! 32/75 — @paulfioravanti

  33. [1, 2, 3] 33/75 — @paulfioravanti

  34. ["a","b","c"] 34/75 — @paulfioravanti

  35. ["a","a","a","b","c","c"] 35/75 — @paulfioravanti

  36. ["a","a","a","b","c","c"] ⬇ %{"a" => 3,"b" => 1,"c" => 2} 36/75

    — @paulfioravanti
  37. 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
  38. ["a","a","a","b","c","c"] ⬇ %{"a" => 3,"b" => 1,"c" => 2} 38/75

    — @paulfioravanti
  39. reduce 39/75 — @paulfioravanti

  40. ! 40/75 — @paulfioravanti

  41. Enum.reduce(enumerable, acc, fun) 41/75 — @paulfioravanti

  42. Enum.reduce(["a", "a", "a", "b", "c", "c"], acc, fun) 42/75 —

    @paulfioravanti
  43. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, acc,

    fun) 43/75 — @paulfioravanti
  44. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, accumulator,

    fun) 44/75 — @paulfioravanti
  45. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, acc,

    fun) 45/75 — @paulfioravanti
  46. list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, acc,

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

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

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

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

    fn char, map -> # ... # ... # ... end) #=> %{"a" => 3, "b" => 1, "c" => 2} 50/75 — @paulfioravanti
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. EVERY Enum FUNCTION IS reduce 57/75 — @paulfioravanti

  58. Enum.map([1, 2, 3], fn number -> number * 2 end)

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

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

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

    2 | list] end) |> Enum.reverse() #=> [2, 4, 6] 61/75 — @paulfioravanti
  62. 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
  63. SAME 63/75 — @paulfioravanti

  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. SAME 70/75 — @paulfioravanti

  71. ALL ARE FOR reduce 71/75 — @paulfioravanti

  72. 72/75 — @paulfioravanti

  73. ! 73/75 — @paulfioravanti

  74. ! 74/75 — @paulfioravanti

  75. Thanks! @paulfioravanti