Created
November 15, 2018 16:53
-
-
Save fay-jai/6dc342a87147669d1237f875a25f86e6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Shorthand notation for anonymous functions in Elixir | |
sum = fn (a, b) -> a + b end # This is the normal notation for anonymous functions in Elixir | |
sum = &(&1 + &2) # This is the shorthand notation. | |
# The initial & is equivalent to the `fn` and `end` keywords. | |
# Within the parentheses, &1 refers to the first parameter, and &2 refers to the second parameter. | |
# If your anonymous function has more parameters, you can continue naming them in the same way. | |
# Just for comparison, here's the normal and shorthand notation for anonymous functions in JavaScript. | |
const sum = function (a, b) { return a + b; }; # Normal notation | |
const sum = (a, b) => a + b; # Shorthand notation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment