Difference between anonymous and named functions?

Can Someone please explain difference between anonymous and named functions ? where we need to use Anonynous and Named functions?

From the Agents tutorial they use

@doc “”"
Gets a value from the bucket by key.
“”"
def get(bucket, key) do
Agent.get(bucket, &Map.get(&1, key))
end

in the above example they have used named function Map.get/3 as Anonymous function by using Capture opertaor. Is there any particular reason to use ?

an anonymous function is not attached to a module.

example

my_func = fn x -> IO.puts x end

and You call it like this

my_func.(“yo”)

Please note the dot

Map.get is a named function

Is there Any particular reason to use named function Map.get/3 as anonymous function as &Map.get(&1, key)?

Passing a function is so usual, that there is this & &1 notation, but it does not mean that Map.get is anonymous.

You can do like something like this with a more complex function

do_what_you_want = fn x -> # do some stuff end
map |> Enum.map(&do_what_you_want.(&1))

Otherwise, You should have done

map |> Enum.map(fn x -> # do some stuff end)

And note that

&Map.get(&1, key)

is equivalent to

fn x -> Map.get(x, key) end

It is not equivalent to Map.get(x, key)

1 Like

Thanks man , i found this stack page with more info, but i am not clear about

This code will fail to compile because every time you see a def, you get an empty variable scope. That is an important difference between them

what is mean by empty variable scope?

Well, simply that there are no variables bound in the scope.

from this example

x = 1
def hello(y) do
x + y
end

after we declare def, do u mean that only y is bound to the scope of the function alone

iex(1)> a = 1
1
iex(2)> f = fn -> IO.puts a end
#Function<...>
iex(3)> f.()
1
:ok
iex(4)> defmodule F do
...(4)>   def g, do: IO.puts(a)
...(4)> end
** (CompileError) iex:5: function a/0 undefined
... stripped ...

As you can see here, the defed function has no access to the a that was bound outside, its scope is therefore “empty”

1 Like

As a statement that is incorrect. An anonymous function is attached to the module that creates it [source].

Execution of the anonymous function will force the module that created it to load - and if that module cannot be loaded, execution of the anonymous function will fail - no matter how simple that anonymous function might be.

Example session:

# File: alpha.ex
defmodule Alpha do

  def make_fun,
    do: fn -> "Greetings from module #{__MODULE__}" end

end

# File: bravo.ex
defmodule Bravo do
  def dessicate(fun, path) do
    File.write path, (:erlang.term_to_binary fun), [:binary]
  end

  def hydrate(path) do
    {:ok, content} = File.read path
    :erlang.binary_to_term content
  end
end

iex(1)> ls()
     alpha.ex
     bravo.ex
iex(2)> c("alpha.ex")
[Alpha]
iex(3)> c("bravo.ex")
[Bravo]
iex(4)> :code.is_loaded Alpha
{:file, :in_memory}
iex(5)> :code.is_loaded Bravo
{:file, :in_memory}
iex(6)> fun = Alpha.make_fun
#Function<0.111841791/0 in Alpha.make_fun/0>
iex(7)> fun.()
"Greetings from module Elixir.Alpha"
iex(8)> Bravo.dessicate fun, "greetings"
:ok
iex(9)> 
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a
$ iex
iex(1)> ls()
      alpha.ex
      bravo.ex
      greetings
iex(2)> c("bravo.ex")
[Bravo]
iex(3)> :code.is_loaded Alpha
false
iex(4)> :code.is_loaded Bravo
{:file, :in_memory}
iex(5)> fun = Bravo.hydrate "greetings"
#Function<0.111841791/0 in Alpha>
iex(6)> fun.()
** (UndefinedFunctionError) undefined function
    #Function<0.111841791/0 in Alpha>()
iex(6)> c("alpha.ex")              
[Alpha]
iex(7)> fun.()       
"Greetings from module Elixir.Alpha"
iex(8)> :code.is_loaded Alpha          
{:file, :in_memory}
iex(9)> c("alpha.ex",".")              
warning: redefining module Alpha (current version defined in memory)
  alpha.ex:2
[Alpha]
iex(10)> c("bravo.ex",".")
warning: redefining module Bravo (current version defined in memory)
  bravo.ex:2
[Bravo]
iex(11)> ls()
     Elixir.Alpha.beam
     Elixir.Bravo.beam
     alpha.ex
     bravo.ex
     greetings
iex(12)> 
BREAK: (a)bort (c)ontinue (p)roc info (i)nfo (l)oaded
       (v)ersion (k)ill (D)b-tables (d)istribution
a
$ iex
iex(1)> ls()
     Elixir.Alpha.beam
     Elixir.Bravo.beam
     alpha.ex
     bravo.ex
     greetings
iex(2)> r(Bravo)
warning: redefining module Bravo (current version loaded from Elixir.Bravo.beam)
  bravo.ex:2

{:reloaded, Bravo, [Bravo]}
iex(3)> :code.is_loaded Alpha
false
iex(4)> :code.is_loaded Bravo
{:file, :in_memory}
iex(5)> fun = Bravo.hydrate "greetings"
#Function<0.111841791/0 in Alpha>
iex(6)> :code.is_loaded Alpha          
false
iex(7)> fun.()
"Greetings from module Elixir.Alpha"
iex(8)> :code.is_loaded Alpha
{:file, '/Users/wheatley/sbox/elx/trial/anon/Elixir.Alpha.beam'}
iex(9)>
2 Likes