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 aware of 2 such projects, are there others?
git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
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
Hi everyone.
My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
Hello folks!
So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
How Can I Optimise Compile Time Dependencies
I have been building an elixir application for about 2 years now. Many modules and files ha...
New
Other Trending Topics
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
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
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
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with.
But I don’t use it anymore.
Serverless was the promis...
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
- #ai
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #security
- #performance











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