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
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
ICal is a library for interacting with iCalendar data. It parses iCalendars into typed Elixir structs via ICal.from_ics, and can prepare ...
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
- #elixir-ls
- #blog-post
- #ai
- #phoenix_html
- #elixirconf-us
- #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: