belteconti
Hey! Basically, what I am trying to do is update a boolean variable inside an if statement. What I’m currently doing is the following:
defmodule MyModule do
def my_function() do
curr1 = curr2 = true
if a > b do
curr1 = false
else
curr2 = false
curr1 or curr2
end
end
end
The problem over here is that neither curr1 nor curr2 gets updated. Does anyone know we can update the values?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
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
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
apply_graft/2 doesn’t rewrite an add_many sub-workflow’s deps on an add step. Grafted jobs cancel with “upstream job was deleted”
Version...
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
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
In Elixir, everything is an expression.
Other than in languages that have mutation, variables are not ‘boxes you put something into’ but rather ‘labels you attach to a value’.
As such you can change what a label points to, but this will not alter the original value (rather ‘you move the label’ you could say).
This means that rather than overriding variables as you attempt to do here, try to return values from your if-statement directly.
For instance:
belteconti
This is excellent advice. Thank you! One additional thing that I would like to ask is that what if we are using this if condition inside an
Enum.mapand thecurr1 or curr2part has to come after theEnum.mapsection. Over here, it won’t recognizecurr1andcurr2now. For example:Any help over how we can work around over here?
LostKobrakai
Elixir is lexically scoped, so variables of outer scopes are accessible to inner scopes (though not changeable as you’ve seen). Anonymous functions, branching (case/if) or blocks like do/end create new scopes. In your case you try to access variables of an inner scope from the outside, which is not possible. If you need values from inside the anonymous function you need to figure out a way to have it returned by the
Enum.mapor other function call, which executes the anonymous function.kokolegorille
It’s always false (when i is a positive number)
The code is wrong, but it would always be true
al2o3cr
Rebinding variables (what happens when you write
a = 1followed bya = 2later in the same block) is always scoped to the current block and does not “leak” into the surrounding context. That’s why your first example doesn’t work:Same thing for the
Enum.mapcase; each invocation of the anonymous function passed toEnum.mapbindscurr1andcurr2and then those bindings are dropped at the end of the function.Depending on your specific needs, a function like
Enum.any?or similar might be a better fit; alternatively,Enum.reducewill do what you’re looking for:Enum.reduceprovides the equivalent of an “update” to the accumulated state as the list is traversed.belteconti
Thank you! This helped a lot and I was able to make it work using
Enum.reduceandMaps.