jgonet
Popcorn Core Team
I was creating simple RPN calculator function, but I stumbled upon a problem in matching functions in guards.
Code looks like so:
def compute_RPN_expr(value, stack) when is_number(value), do: [value | stack]
def compute_RPN_expr(operator, stack) when operator in [&+/2, &-/2, &*/2, &//2] do
[a, b | stack] = stack
stack = [operator.(b, a) | stack]
end
After trying to define it inside IEX I’m getting invalid expression in guard, & is not allowed in guards.
Why? I can do:
a = &//2
a === &//2
Without much hassle, but I can’t use the same equality test via in.
Is there any Elixir/Erlang limitation or this was simply overlooked?
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
New
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
In general you can’t compare functions for equality. So even if it were syntactically allowed, it wouldn’t make sense semantically.
Instead you should use atoms as operators and map them to functions.
jgonet
Why so? This would work like a comparing function pointers in C - checking their address. I’m kinda expecting doing most operations on normal variables will work on functions too. So, for me it makes sense if I’m passing function
fas argument and inside some function I’m checking if I actually passed exactlyf. I know this thing isn’t broadly used, but in some applications can be useful. Mapping atoms to functions creates another level of indirection, but I’ll give it a go.NobbZ
There are no function pointers in elixir.
&+/2is different in every module you write this. Also, even though both of the following functions are obviously producing the same result for their respective domains, you can’t computationally compare them for equality without doing manual prooves:Both of these functions are “equal” to
*/2, but for the latter it will be very hard to proof in a language like elixir.Just rewrite your dispatcher to use atoms (or accept all functions with arity 2).
Qqwy
One important reason is hot code reloading and the distributed kind of Erlang/Elixir programs: If you are running code on multiple nodes, then they both have separate versions of the functions that might be executed (and if you try to upgrade both nodes to a new version, they are not guaranteed to upgrade at exactly the same time). If you pass on a function reference from one node to the next, it has to be transformed to an intermediate representation on one side, and back on the other side.
In short: The reference on one node will not point to the same location as the one on the other node. This means that we cannot check for equality (which is stronger than, but would infer, equivalence, which is what your code actually requires) by comparing pointers.
In Erlang/Elixir, all data structures can be checked for structural equivalence by matching on them. However, functions are, for the reason outlined above, opaque, so we cannot check their internals.
And therefore, an extra symbolic intermediate step like what @NobbZ proposes is required.
In a more general sense, you can never prove the equivalence of two functions, because this would be the same as solving the Halting Problem. The only thing you could do is to prove that two functions are equivalent because they are equal (exactly the same instance), but this is something that is not possible in Elixir because of above-outlined restrictions imposed by the distributed nature of Elixir’s runtime system.
jgonet
I don’t want to prove that function A is equals to function B. I Just want to compare them by address, e.g.
Those 2 functions above aren’t the same, even when they’re doing the same thing. So
a != b.is_function/2may be good idea, I’ll try it, thanks.@Qqwy, thanks for explanation; I’ve got one question: is any tutorial somewhere which covers Elixirs internals or I have to learn Erlang/BEAM low level stuff and then translate to Elixir from source code?
Qqwy
@jgonet There are not many cases in which you actually touch the Erlang/BEAM low-level stuff directly or have to keep them in mind (most of the time, Elixir’s abstractions are very leak-free). However, I would recommend watching Solid Ground because it will give you a good high-level foundation of understanding what trade-offs have been made in Erlang/BEAM
.
benwilson512
The BEAM Book: Understanding the Erlang Runtime System is a good resource for looking at the internals of how the BEAM itself works. Decisions and capabilities like the one you’re describing are gonna either be present or absent on the basis of the BEAM itself, Elixir just gets compiled to BEAM byte code.
7stud
Why is
:erlangthe module in MFA.LostKobrakai
You could use
Kernel, but those just forward to the erlang implementations anyways: elixir/lib/elixir/lib/kernel.ex at 8ca85ed2a82019df1a2acf3238f24a56370464bb · elixir-lang/elixir · GitHub