Add elixir lambda
This commit is contained in:
parent
8f0586947a
commit
c6d41dad1a
2 changed files with 33 additions and 0 deletions
32
src/dev/elixir/lambda.md
Normal file
32
src/dev/elixir/lambda.md
Normal file
|
@ -0,0 +1,32 @@
|
|||
# Lamba or Anonymous Functions
|
||||
|
||||
## Create an anonymous function and bind it to a variable
|
||||
|
||||
### Simple one
|
||||
|
||||
```elixir
|
||||
iex> func = fn -> IO.puts("Hello") end
|
||||
#Function<21.126501267/0 in :erl_eval.expr/5>
|
||||
iex> func.()
|
||||
Hello
|
||||
:ok
|
||||
```
|
||||
|
||||
### One with arguments
|
||||
|
||||
```elixir
|
||||
iex> func = fn t -> IO.puts(t) end
|
||||
#Function<7.126501267/1 in :erl_eval.expr/5>
|
||||
iex> func.("Hello")
|
||||
Hello
|
||||
:ok
|
||||
```
|
||||
|
||||
Another solution is the `&` operator used as syntastic sugar
|
||||
|
||||
```elixir
|
||||
iex> func = &(&1 + &2)
|
||||
&:erlang.+/2
|
||||
iex> func.(2, 2)
|
||||
4
|
||||
```
|
Loading…
Add table
Add a link
Reference in a new issue