hipertracker
How to write idiomatic Elixir for the following code in Ruby? The main difficulty here is that the immutable language cannot accumulate items in such way: if my current item does not meet the condition than I want to get the last item which did it before.
result = []
items.each do |item|
if condition(item)
result << {value: item, flag: :ok}
else
result << {value: result.last, flag: :previous}
end
end
UPDATE: I had an error in the code. I have to find last item from result list.
Trending in Questions
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
How do I tell Hologram not to escape HTML when substituting into templates?
{Textile.render(@product.long_description)}
escapes the HTM...
New
Background
When trying to execute mix release on a Windows 11 machine for a Phoenix project I get the following error:
* assembling mark...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
Is there a way to import record definitions in the iex shell for pretty printing (similar to the rr command in the erl shell)? This woul...
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
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly)
AI dream has many faces, but general...
New
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
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
- #code-sync
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #ai
- #elixirconf-us
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #blog-post
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #security
- #elixirconf-eu
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
bbense
There’s a couple approaches, if items is always a List, you can write your own reduction. If it might be something else that is still Enumerable, you’ll need to use Enum.reduce. Basically, all “looping” is done with reductions that peel off the head of the loop and then call the function on the tail. If you need state from a previous loop, you put
that in an “acc” or accumulator argument.
Here’s a list based version
Depending on exactly what you want to do, you might need to reverse the output of last_true at this point. There is an optimized erlang function for this case.
:lists.reverse. You would call last_true something like this.hipertracker
I had the error in the example. It should be
result.last. This is a dynamic created list, it does not exist outside the main loop. And this is the main difficulty. Maybe I am wrong, but Elixir has no access to the internal built list.bbense
That’s exactly what my code does. It builds the list item by item and passes it on to the next step of the reduction. In each step of the recursion, acc contains the previously build version of the results list.
peerreynders
Are you sure? Because to me it looks like
{value: result.last, flag: :previous}is going to look something like
{value: {value: item, flag: :ok}, flag: :previous}or worse if nested even further (though maybe there is some subtlety that I’m missing).
Though some of the pattern matching could be relocated
.
andre1sk
I am not sure that your code matches your desc. The equivalent to your ruby code would be something like:
peerreynders
Again not accounting for the fact that the first element in the list could fail the condition leading to
hd([])Nice and short though (personally in this case I think the capture operator messes with readability; just as long as everybody remembers that
&&and||are short-circuit operators, not boolean operators).or more verbosely:
andre1sk
you are totally right
should be Enum.at() with default nil as opposed to hd.
peerreynders
I know you are talking about
Enum.at(list,0)- interestingly enoughEnum.at(list,-1)will get the last element of a list - however in general indexed access on a list (short of to the head like you are suggesting) doesn’t feel like idiomatic FP to me.benwilson512
What is the correct ruby code you’re trying to emulate?