Last active
September 14, 2022 16:09
-
-
Save tusqasi/dd01f001db72d04799acb712d27a23e7 to your computer and use it in GitHub Desktop.
Useful Enum functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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