belteconti
How to update boolean value of a variable inside if statements?
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?
Most Liked
al2o3cr
Rebinding variables (what happens when you write a = 1 followed by a = 2 later 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:
def my_function() do
curr1 = curr2 = true
if a > b do
# This _rebinds_ the name "curr1" but only until the "else"
curr1 = false
else
curr2 = false
end
# This refers to the original binding, not the one introduced inside the "if" blocks
curr1 or curr2
end
Same thing for the Enum.map case; each invocation of the anonymous function passed to Enum.map binds curr1 and curr2 and 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.reduce will do what you’re looking for:
# Ruby version, that uses "leaky" scopes
curr1 = nil
curr2 = nil
some_list.map { |el| curr1, curr2 = something_with(el, curr1, curr2) }
# read the last values stored in curr1 and curr2 here
# Elixir version that makes the shared state between iterations explicit
{curr1, curr2} = Enum.reduce(some_list, {nil, nil}, fn el, {a1, a2} -> something_with(el, a1, a2); end)
Enum.reduce provides the equivalent of an “update” to the accumulated state as the list is traversed.
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.map or other function call, which executes the anonymous function.
belteconti
Thank you! This helped a lot and I was able to make it work using Enum.reduce and Maps.
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








