Improve references around functions

This commit is contained in:
José Valim
2020-05-17 16:05:45 +02:00
parent d2362e54c7
commit d98e532b6c
2 changed files with 22 additions and 4 deletions
+20 -4
View File
@@ -8,8 +8,21 @@ defmodule Function do
iex> add.(1, 2)
3
It is also possible to capture module functions and pass them around
as if they were anonymous functions by using the capture operator `&/1`:
Anonymous functions can also have multiple clauses. All clauses
should expect the same number of arguments:
iex> negate = fn
...> true -> false
...> false -> true
...> end
iex> negate.(false)
true
## The capture operator
It is also possible to capture public module functions and pass them
around as if they were anonymous functions by using the capture
operator `Kernel.SpecialForms.&/1`:
iex> add = &Kernel.+/2
iex> add.(1, 2)
@@ -19,8 +32,9 @@ defmodule Function do
iex> length.("hello")
5
It is also possible to capture a definition in the current module by
skipping the module prefix, such as `&my_fun/2`.
To capture a definition within the current module, you can skip the
module prefix, such as `&my_fun/2`. In those cases, the captured
function can be public (`def`) or private (`defp`).
The capture operator can also be used to create anonymous functions
that expect at least one argument:
@@ -31,6 +45,8 @@ defmodule Function do
In such cases, using the capture operator is no different than using `fn`.
## Internal and external functions
We say that functions that point to definitions residing in modules, such
as `&String.length/1`, are **external** functions. All other functions are
**local** and they are always bound to the file or module that defined them.
+2
View File
@@ -1559,6 +1559,8 @@ defmodule Kernel.SpecialForms do
@doc """
Defines an anonymous function.
See `Function` for more information.
## Examples
iex> add = fn a, b -> a + b end