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

greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
tduccuong
Hi, is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Other popular topics Top

albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 36352 110
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

We're in Beta

About us Mission Statement