Skip to content

Instantly share code, notes, and snippets.

@tusqasi
Last active September 14, 2022 16:09
Show Gist options
  • Save tusqasi/dd01f001db72d04799acb712d27a23e7 to your computer and use it in GitHub Desktop.
Save tusqasi/dd01f001db72d04799acb712d27a23e7 to your computer and use it in GitHub Desktop.
Useful Enum functions
final_val = Enum.reduce([10, 22, 84], 0, fn(x, acc) -> x + acc end)
# acc takes the value of the return of function passed to Enum.reduce
# `here fn(x,acc) -> x + acc` is the function
# 1
# fn(10, 0) returns 10
# 2
# fn(22, 10) returns 32
# 3
# fn(84, 32) returns 116
# https://hexdocs.pm/elixir/Enum.html#reduce/2
new_list = Enum.map([7, 2, 4], fn x -> x * 2 end)
# each element of the list/iterator is replaced with the return of the function
# in place of 7, x*2 = 14 is placed
# in place of 2, x*2 = 4 is placed
# in place of 8, x*2 = 8 is placed
# https://hexdocs.pm/elixir/Enum.html#map/2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment