hariharasudhan94

hariharasudhan94

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 ?

First Post!

kokolegorille

kokolegorille

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

Most Liked

peerreynders

peerreynders

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)>
kokolegorille

kokolegorille

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)

NobbZ

NobbZ

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”

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement