kalindkaria
Hi, I’ve just started learning Elixir.
For one of the applications I’m developing, I have the following code:
use Bitwise
sensor_data = %{sensor1: 0}
for n <- 0..9, do: sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< n)))
This produces following output:
[
%{sensor1: 1},
%{sensor1: 2},
%{sensor1: 4},
%{sensor1: 8},
%{sensor1: 16},
%{sensor1: 32},
%{sensor1: 64},
%{sensor1: 128},
%{sensor1: 256},
%{sensor1: 512}
]
But the expected output should be:
[
%{sensor1: 1},
%{sensor1: 3},
%{sensor1: 7},
%{sensor1: 15},
%{sensor1: 31},
%{sensor1: 63},
%{sensor1: 127},
%{sensor1: 255},
%{sensor1: 511},
%{sensor1: 1023}
]
If I run the following statement in iex session the sensor_data map gets updated like this:
sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< 0)))
%{sensor1: 1}
sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< 1)))
%{sensor1: 3}
sensor_data = Map.update!(sensor_data, :sensor1, &(&1 ||| (1 <<< 2)))
%{sensor1: 5}
Can you help me understand why does the Map.update!/3 doesn’t work inside a for loop?
I also tried with Map.update/4 Map.replace!/3 but no luck.
Trending in Chat/Questions
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
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
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
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #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)
stefanchrobot
The “assignment” inside the for loop has no effect, it creates a new variable on each loop iteration. I think you might be looking for
Enum.reduce:stefanchrobot
See also: https://elixir-lang.org/getting-started/case-cond-and-if.html#if-and-unless
kalindkaria
Thanks for your reply.
But in my case, the
forloop control or thenis controlled from some other function and I need to update thesensor_datavariable based on the value ofn.I did try using
Enum.reducebefore but they can’t with an externalforloop as it takes the control of iteratingnfrom0..9.LostKobrakai
If you really want to use
foryou can. Since a few versions back it also supports reductions.kokolegorille
Not only, You also need the previous value.
That’s why You need the acc of a reducer…
kalindkaria
Thanks @LostKobrakai and @kokolegorille !
I missed out this
:reduceoption in Comprehensions. I am able to recall now.Documentation about
:reducefor anyone to refer in future: