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
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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
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
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
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 9 to 1- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
What is the correct ruby code you’re trying to emulate?
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.andre1sk
you are totally right
should be Enum.at() with default nil as opposed to hd.
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
I am not sure that your code matches your desc. The equivalent to your ruby code would be something like:
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
.
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.
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
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.