aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible.
total = 10
while total != 0
puts "hello"
total -= 1
end
The question and answer in this example use list for loop. Convert ruby while loop into elixir
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
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 everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
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
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
I found
Stream.unfoldto be quite useful for places where I don’t know how many iterations are needed to get to the end.There’s also
Enum.reduce_while, but it needs an enumerable as input to start with.DanCouper
Something like this?
tme_317
I think recursion is the simplest way to go…
You could use
ifinstead ofcasesince there are only two patterns.Alternatively:
peerreynders
Refactoring: Replace Iteration with Recursion
Refactoring: Replace Recursion with Iteration
Recursion to Iteration Series
Recursion? We don’t need no stinking recursion!
Zesky665
Try this
sribe
I just want to comment on something, at first glance all the solutions posted on this page look kind of complicated for such a simple thing. But you almost never need to write such code in the real world.
In actual apps, you want to iterate over some collection of data items. The
fororwhileloop is how you do that in an imperative language: you have some “iterator” kind of value–a simple index for an array, something else for a map or set–increment that, check if it’s out of range, use it to access your data collection.In functional languages, you pass the operation you want to perform on the data as an argument to one of the collection’s functions. In Elixir,
Enum.maporEnum.reduce. This actually results in more compact code because it eliminates the whole “get an iterator, increment, check it, use it” dance.So while this particular exercise can help you understand some low-level mechanics of Elixir, it could also be misleading. Please don’t think you have to jump through the hoops of all these solutions in order to do something with an array of values you get back from your database or submitted from a web form
That said, here is my solution:
Enum.each(10..1, fn(i) -> IO.puts(i) end)or, using some shorthand:
Enum.each(10..1, &(IO.puts("#{&1}")))or, if you want to prove you actually understand passing around functions:
Enum.each(10..1, &IO.puts/1)But even that could be misleading, because in a functional language you don’t often just “run a loop” over a collection for side effects, you usually produce a value, either a set of values produce from each source value (
Enum.map) or a single value derived from the whole set (Enum.reduce).sribe
Almost, but not quite
And in real life, you probably want
def while(x) when x > 0 dosribe
@Zesky665 Haha, you & I were correcting at the same time!
peerreynders
However by bypassing the “low-level mechanics” (and going straight for higher order functions) you are giving up on the opportunity to explore the general connection (and differences) between recursion and iteration, for example:
Granted that lesson seems more important in an environment where mutability is a possible choice - where it might be prudent to be “Immutable where possible, mutable (only) when needed”.
[**] which is how the basic HOFs operate.
Another matter is that a
whileloop isn’t just one single concept. There is the idea ofSo the concept of a
whileloop may actually be seen as more basic than it actually is just because certain programming languages offer a single statement or expression as a representation for it.Zesky665
You’re right, I always forget that you can just leave a function empty and it won’t cause an error.