bartblast

bartblast

Creator of Hologram

Anonymous functions equality comparison

Consider the following code:

iex> fn1 = fn x -> x end
#Function<42.3316493/1 in :erl_eval.expr/6>

iex> fn2 = fn x -> x end
#Function<42.3316493/1 in :erl_eval.expr/6>

iex> fn1 == fn2
false

iex> m = %{fn1 => 123}
%{#Function<42.3316493/1 in :erl_eval.expr/6> => 123}

iex> m[fn2]
nil

Is there some way to determine whether 2 anonymous functions are equal? By equal we can mean having the same underlying AST or being equivalent in some way.

Another related question:
what is “42.3316493/1” and is it possible to get this number programmatically somehow?

Marked As Solved

jhogberg

jhogberg

Erlang Core Team

Two anonymous functions are equal if they were created from the same definition (same fn ... statement) in the same module (same checksum), and have the exact same environment variables.

Funs defined in the shell satisfy the first two as long as their arity is the same, but differ in the latter as they’re interpreted by the shell which drags in a ton of stuff to their environment. If you want consistent results please try this with compiled modules instead.

Also Liked

jhogberg

jhogberg

Erlang Core Team

Yes.

Yes, because you’re consistently comparing different definitions. Different fn ... end statements yield different funs. If you were to compare the result of fun_1 to the result of another invocation of fun_1, they would compare equal.

LostKobrakai

LostKobrakai

Function.info can do that.

KristerV

KristerV

you sound like you know what you’re doing, but i feel like asking what you’re trying to accomplish anyway :slight_smile:

bartblast

bartblast

Creator of Hologram

By “environment variables” do you mean the variables captured in the closure of this anonymous function?

Using the following compiled code:

defmodule MyModule do
 def fun_1 do
   fn x -> x end
 end

 def fun_2 do
   fn x -> x end
 end

 def compare_1 do
   ((fn x -> x end) == (fn x -> x end)) |> IO.inspect()
 end

 def compare_2 do
   (fn x -> x end) |> Function.info() |> IO.inspect()
   (fn x -> x end) |> Function.info() |> IO.inspect()
   :ok
 end
end

iex -S mix:

iex> MyModule.fun_1() |> Function.info()
[
  pid: #PID<0.252.0>,
  module: MyModule,
  new_index: 0,
  new_uniq: <<93, 38, 53, 241, 226, 42, 107, 201, 80, 47, 228, 61, 192, 123,
    202, 1>>,
  index: 0,
  uniq: 48837039,
  name: :"-fun_1/0-fun-0-",
  arity: 1,
  env: [],
  type: :local
]
iex> MyModule.fun_2() |> Function.info()
[
  pid: #PID<0.252.0>,
  module: MyModule,
  new_index: 1,
  new_uniq: <<93, 38, 53, 241, 226, 42, 107, 201, 80, 47, 228, 61, 192, 123,
    202, 1>>,
  index: 1,
  uniq: 48837039,
  name: :"-fun_2/0-fun-0-",
  arity: 1,
  env: [],
  type: :local
]
iex> MyModule.compare_1()
false
iex> MyModule.compare_2()
[
  pid: #PID<0.251.0>,
  module: MyModule,
  new_index: 2,
  new_uniq: <<122, 234, 246, 244, 241, 108, 55, 10, 70, 250, 99, 174, 106, 44,
    101, 226>>,
  index: 2,
  uniq: 64444343,
  name: :"-compare_2/0-fun-0-",
  arity: 1,
  env: [],
  type: :local
]
[
  pid: #PID<0.251.0>,
  module: MyModule,
  new_index: 3,
  new_uniq: <<122, 234, 246, 244, 241, 108, 55, 10, 70, 250, 99, 174, 106, 44,
    101, 226>>,
  index: 3,
  uniq: 64444343,
  name: :"-compare_2/0-fun-1-",
  arity: 1,
  env: [],
  type: :local
]
:ok

Looks like the functions will always differ at least in new_index or index (?)

bartblast

bartblast

Creator of Hologram

I think I understand, thank you!

Where Next?

Popular in Questions Top

quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
Student &amp; New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement