benhoven
Hi Everybody,
Recently I discovered interesting use case for reduce_while that does not use acc as accumulator → it doesn’t carry / use the variable in subsequent loops.
Could you please take a look and tell me if it’s clever or stupid?
Enum.reduce_while(some_enumerable, {:error, :show_this_when_empty_input}, fn some_item, _ ->
with {:ok, something} <- something(some_item),
{:ok, something} <- something_else(something) do
{:halt, {:ok, something}}
else
# known error - continue OR finish with this error
{:error, :expected_message} = error -> {:cont, error}
# unknown error - halt immediately
{:error, _} = error -> {:halt, error}
end
end)
What I can get from that is:
{:error, :show_this_when_empty_input}when enumerable is an empty list{:error, :expected_message}when no element in enumerable passes the test (the function) in 3rd argument{:ok, result}first positive result from the function → first element that passes and then stop processing{:error, anything}for results / errors that are not expected
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,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
I’ve been experimenting with Jev for classification and small decision tasks, and wanted something that felt natural to use in Elixir app...
New
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 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
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
wolf4earth
I’ve written code similar to this, so yeah, I think that’s a perfectly reasonable usage of
Enum.reduce_while.Eiji
Your code looks good except one point. Calling
Enum.reduce*functions does not makes sense if you do not need an accumulator. I would rather write something like:For more information please take a look at Enum.find_value/2 documentation.
kokolegorille
I would also try to move the code to Enum.find, or Enum.find_value, but the condition is more complex, and not really a truthy kind of condition.
It’s hard to capture the with, with Enum.find
Eiji
Sorry, I did not get it. Since we accept any function and expect not
nilall mentioned clauses could be changed to return “something” like{:error, error}or{:ok, result}and optionally for{:error, :expected_message}we may want to returnnilin order to skip specific item.If you want you can also write a really simple few-lines function like:
and use it like:
Look that in
caseclauses you may simply add support for{:halt, acc}and{:cont, acc}if you do not want to change anonymous function from an original post. You may even combineExample.sample/3with such anonymous function.kokolegorille
I know recursion could solve this problem quite elegantly.
What I meant is Enum.find_value is not working well in that case.
Because the condition should exit only if ok, or unexpected error… and continue if the error is known. But You need to return nil/or false instead, to continue iteration
benhoven
Thank you very much @Eiji, I took you example
sampleand I created a functional example / comparison of my old cold and your recommended new code with recursion.Just one small correction - second
samplehasdo: defaultand I didn’t use default value fordefaultargument.That would basically create
.
Enum.reduce_whilethat doesn’t haveacc. Very cool