Add elixir lambda

This commit is contained in:
Wilfried OLLIVIER 2020-03-28 10:32:24 +01:00
parent 8f0586947a
commit c6d41dad1a
2 changed files with 33 additions and 0 deletions

View file

@ -7,6 +7,7 @@
- [Golang](./dev/golang/main.md)
- [Testing](./dev/golang/testing.md)
- [Elixir](./dev/elixir/main.md)
- [Lambda](./dev/elixir/lambda.md)
- [GenServer](./dev/elixir/genserver.md)
- [Dates](./dev/elixir/dates.md)
- [Rust](./dev/rust/main.md)

32
src/dev/elixir/lambda.md Normal file
View 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
```