docjazither

docjazither

Best way to find which index in Enum.any? has satisfied its condition

Hi OGs,
Any ideas how to find the best way to find which index in Enum.any? has satisfied its condition ?

I have to iterate through a list and delete everything except one, I got fed with multiple lists and based on conditions, I would spare the last one in each list differently

The reason why I can’t iterate through a list and find the one I want to delete is due to the complexity of conditions, I need to know who they are, every single one of them in my list. For example, [%A{}, %B{}] I could just delete A as required, but [%A{} ,%B{} , %C{} , %D{}] I need to know C has what, D has what in order to delete A or else I have to delete C or D. That’s why I need to know which indexes are A, C, D through Enum.any?

Thank you

Marked As Solved

gpopides

gpopides

Enum.any?/2 returns truth if the function returns true for any of the elements. You could use Enum.find_index/2 in similar fashion, but i see that you need 3 indexes instead of 1.

Could you use something like this?

Enum.with_index(enumerable)
|> Enum.map(fn {value, index} ->
 {filter_function_result(value), index}
 end)
|> Enum.filter(fn {filter_value_result, index} -> filter_value_result == true)

This would return a list of tuples with the filter value result (booleans) and their indexes, so you can remove the results too if you want

Also Liked

trisolaran

trisolaran

you’re looking for: Enum.find_index/2

trisolaran

trisolaran

Expanding on @gpopides’s idea, you can get the value and the index of all elements that satisfy the condition with this:

enumerable
|> Enum.with_index()
|> Enum.filter( fn {value, index} -> filter_function(value) end )

Where Next?

Popular in Questions Top

lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
kostonstyle
Hi all I want to have a unix time, from the current time plus 1 hour. DateTime.now + 1 hour How to get it in elixir? Thanks
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New

Other popular topics Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New

We're in Beta

About us Mission Statement