Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Anonymous Functions

3_anonymous_functions.livemd

Anonymous Functions

import IEx.Helpers

Anonymous Function

# format of anonymous function
# fn x -> x +1 end
inc = fn x -> x + 1 end
#Function<42.105768164/1 in :erl_eval.expr/6>
i(inc)
Term
  #Function<42.105768164/1 in :erl_eval.expr/6>
Data type
  Function
Type
  local
Arity
  1
Description
  This is an anonymous function.
Implemented protocols
  Enumerable, IEx.Info, Inspect
inc.(42)
43
func2 = &amp;IO.inspect/1
&IO.inspect/1
i(func2)
Term
  &IO.inspect/1
Data type
  Function
Type
  external
Arity
  1
Implemented protocols
  Enumerable, IEx.Info, Inspect
func2.(42)
42
42
h(Enum.map())

                            def map(enumerable, fun)                            

  @spec map(t(), (element() -> any())) :: list()

Returns a list where each element is the result of invoking fun on each
corresponding element of enumerable.

For maps, the function expects a key-value tuple.

## Examples

    iex> Enum.map([1, 2, 3], fn x -> x * 2 end)
    [2, 4, 6]
    
    iex> Enum.map([a: 1, b: 2], fn {k, v} -> {k, -v} end)
    [a: -1, b: -2]
Enum.map([1, 2, 3], fn x -> x * x end)
[1, 4, 9]
Enum.map([1, 2, 3], fn x ->
  x * x
end)
[1, 4, 9]
Enum.map(
  [1, 2, 3],
  fn x ->
    x * x
  end
)
[1, 4, 9]