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’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
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’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
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
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
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 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.