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
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
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
Aludel - LLM Evaluation Workbench
Aludel is an embeddable Phoenix LiveView dashboard for evaluating and comparing LLM prompts across mult...
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
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










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.