Skip to content

Instantly share code, notes, and snippets.

@cored
Created April 7, 2023 17:59
Show Gist options
  • Save cored/e8cc767e6a2faa0af0c918802757057a to your computer and use it in GitHub Desktop.
Save cored/e8cc767e6a2faa0af0c918802757057a to your computer and use it in GitHub Desktop.
square = fn(x) -> x * x end
# calling an anonymous function uses a period
# before the parentheses
4 = square.(2)
# pattern matching the arguments can be used
first = fn([head|rest]) -> head end
1 = first.([1, 2])
# anonymous functions are commonly used as arguments
# to other functions
[1, 4, 9] = Enum.map([1, 2, 3], square)
[3, 4, 5] = Enum.map([1, 2, 3], fn(x) -> x + 2 end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment