DOKL57
Hi all, the question seems like a simple question, but it has thrown me for a long time. I have a loop in which I loop through an element of the map list and I need to add the current item to the new list.
For a better understanding, here is a simpler example:
current_list = []
for x <- 1..10 do
current_list = [x | current_list]
end
I expect to get [1,2,3,4,5,6,7,8,9,10] in current_list, but got
What am I doing wrong? Thanks in advance!
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
Hey, I know this has been discussed ad nauseum, but I couldn’t find a definitive answer or at least some consensus around this, so let me...
New
I don’t suppose you can lazy load a library depending on runtime data? For example if it’s a certain user they can lazy load ‘library A’ ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
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
- #code-sync
- #exunit
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #blog-post
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Aetherus
The
current_listinside the for-loop is not the same variable as thecurrent_listoutside the for-loop. Actually, thecurrent_listinside each loop is different.I bet you’ve heard that in Elixir, everything is immutable, which means if a list is empty, it stays empty for the rest of its life. You can’t stuff things into it, or remove things from it, or change any of its elements.
So, the correct way is:
or
kokolegorille
or simply
Aetherus
I guess the author may have put something in the
current_listbefore starting the loopIf the
current_listis initially empty, thenEnum.reverse(1..10)should be the shortest code.derek-zhou
IMHO, newbies should stay away from
if,fororwithuntil being comfortable with functional style programming. They are only syntactical sugar to make things shorter; they don’t provide any vital functionalities.christhekeele
IDK,
ifis such a common core construct it feels like it would be off-putting to try to learn a lang where that was discouraged. In fact I’d say it’s the ideal candidate from which to learn this functional scoping lesson!LostKobrakai
Lexical scoping isn’t limited to functional languages. Many languages use that type of scoping.
Usually people don’t expect do/end blocks to be scope boundries though.
christhekeele
Right! The combination of lexical scoping, expression return values, resulting in do/end variables being local and needing to assign out of them, is what’s the useful take-away.
I think
ifis great lesson in this, becauseelsecase; they will have acquired the understanding they need to succeedderek-zhou
A
cond do ...will raise if none of the conditions hold true. This is safer for newbies thanifthat don’t have aelseclause.NobbZ
Yes, though paired with immutability and implicit shadowing it is sometimes a bit weird.
If you do not know about the implicit shadowing, which is dropped again after leaving the
ifs lexical scope, then it is not obvious what happens.The problem is not the scoping, its the shadowing, which is often seen as a bad design decision.
christhekeele
I suppose the compiler could warn on reassignment of shadowed variables. There are legitimate use-cases for it though, especially in more involved conditional bodies. Especially in reduces!