bartblast
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
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
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
Function.info can do that.
KristerV
you sound like you know what you’re doing, but i feel like asking what you’re trying to accomplish anyway ![]()
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









