Paul Fioravanti
July 27, 2017

# 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

July 27, 2017

## Transcript

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

— @paulﬁoravanti
10. ### def reduce([ ! , , ]) do " end 10/75

— @paulﬁoravanti

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

@paulﬁoravanti

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

— @paulﬁoravanti

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

end) #=> 25/75 — @paulﬁoravanti
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 — @paulﬁoravanti

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

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

#=> [2, 4, 6] 31/75 — @paulﬁoravanti

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

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

— @paulﬁoravanti

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

@paulﬁoravanti
43. ### list = ["a", "a", "a", "b", "c", "c"] Enum.reduce(list, acc,

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

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

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

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

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

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

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

fn char, map -> # ... # ... # ... end) #=> %{"a" => 3, "b" => 1, "c" => 2} 50/75 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti

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

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

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

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

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

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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti
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 — @paulﬁoravanti