Skip to content

Instantly share code, notes, and snippets.

@fay-jai
Created November 15, 2018 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fay-jai/6dc342a87147669d1237f875a25f86e6 to your computer and use it in GitHub Desktop.
Save fay-jai/6dc342a87147669d1237f875a25f86e6 to your computer and use it in GitHub Desktop.
# 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