wanton7

wanton7

Is there something like Enum.find_with_index?

I’m trying to look at the docs but I couldn’t find a function that would return element with its index. Is there such a function for lists that doesn’t involve creating a new list? I want to do modify the the element if it exists and then use List.replace_at to replace it. But because I don’t have the index, I first have use Enum.find_index to get the index and then if it’s not nil, then I have to use Enum.at to get the element itself.

Edit: added code

  defp edit_if_exists(elements, id, fun) do
    case elements |> Enum.find_index(&(&1.id == id)) do
      nil ->
        elements

      idx ->
        element = elements |> Enum.at(idx); # Want to know if I can some how avoid this
        elements |> List.replace_at(idx, fun.(element))
    end
  end

Marked As Solved

LostKobrakai

LostKobrakai

Enum.reduce_while(list, {:not_found, 0}, fn el, {:not_found, index} -> 
  if el.id == id, do: {:halt, {:found, index, el}}, else: {:cont, {:not_found, index + 1}}
end)

Also Liked

KristerV

KristerV

this thread would greatly benefit from example code. sounds like a XY problem.

LostKobrakai

LostKobrakai

Can you explain why you need to work with indexes on a list? Often when you need indexed access a list in not the best datatype to use.

LostKobrakai

LostKobrakai

Enum.reduce_while can do that. That’s what find_index is based on.

Where Next?

Popular in Questions Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
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 t...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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 31013 112
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement