In Elixir we can create anonymous function like in javascript, use fn keyword to define an anonymous function.
Variable can be assigned by anonymous function, and you can call it using variable_name.(arg1, arg2)
for example, below is a simple anonymous function that bind into a variable.
Function & Pattern Matching
Pattern matching can be applied in function, we can use pattern matching in function argument to determine which implementation to run. For example lets use tuple for argument.
Please try and guess what the output of the example above. The tuple structure in function argument determines which implementation will be executed. It’s like one function with multiple bodies. Now lets implement fizz-buzz without conditional but using pattern matching:
Can you guess what the output is?