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

Captures

livebook/8_functions/4_captures.livemd

Captures

import IEx.Helpers

Intuition

fun = &(&1 + 1)

# x + 1
#Function<42.105768164/1 in :erl_eval.expr/6>
fun.(42)
43
i(fun)
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
fun2 = &amp;(&amp;1 + &amp;2)
&:erlang.+/2
fun2.(42, 43)
85
fun3 = &amp;(&amp;1 + &amp;1)
#Function<42.105768164/1 in :erl_eval.expr/6>
fun3.(4)
8

Performance

&amp;[&amp;1|&amp;2]

or

fn item, list ->
  [item|list]
end

prefer using anonymous function in elixir

fn item, list ->
  [item|list]
end

over

&amp;[&amp;1|&amp;2]

unless!!!

prefer:

&amp;IO.inspect/1

over:

fn item -> IO.inspect(item) end

prefer &IO.inspect/1

  • initial ampersand marks as capture