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

Combination of Enum.with_index with Enum.find could solve this. like

[:a,:b,:c] |> Enum.with_index() |> Enum.find(fn {char, index} -> char == :b end)

{:b, 1}

Enum.with_index creates a new list

List.replace_at also create a new list, elixir being immutable will always return new data structure for modification.

Alternatively, if you want to focus on replace an element without considering using index, you could just use Enum.map, and change elem if it’s match your search criteria

I don’t care about List.replace_at creating a new list I only care about that finding the element doesn’t create a new list. That why I asked.

Is there such a function for lists that doesn’t involve creating a new list?

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.

1 Like

you could change Enum.with_index to Stream.with_index which wouldn’t make an intermediary list, but at this moment i don’t really understand what you are trying to achieve. (i thought it was replacing modifying an element in list before)

I want to edit element in a list if it exists and not create new list if it doesn’t. Maybe there is a better way?

List in elixir is immutable, changing a list data structure will always create a new list in it.

Maybe list is not the correct data structure for you, if you have special requirement like high performance problem or shared data between process, there’s alternative like :array (Erlang -- array) which give mutable array in elixir.

Of course they are immutable whole language is immutable. I think you have misunderstood what I originally wrote. Like I said I don’t care about that editing a list is creating a new list it’s given. I care about that finding an element is creating a new list.
I can use Enum.find_index and Enum.at if element was found and that doesn’t create a new list. But I wanted to know if there is some function already in Enum or List or somewhere else doesn’t create a new list and returns both element and its index so I could do it in one call instead of two.

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

1 Like

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

3 Likes

Added code to original post.

Couldn’t you reduce over it, accumulating each value unless it matches, when you’d apply the function to it, and keep on accumulating the remaining values?

Or with recursion you could exit when you find it and append the tail to what you’ve accumulated.

Remembering to reverse it at the end.

Thanks, this code will be used for lists of in range of 1000-10000 elements during processing. That’s why I would like finding of the element to be fast and to cause minimal new allocations. Also I would like that the loop returns if element is found to exit fast as possible, so I don’t reduce is the way to go here. Matching of element doesn’t happen that often so fast path needs to be the path that returns nil. It’s not that of big problem currently that I have do extra call Enum.at but asked this question to find out if there is function like that already available that returns both index and the element that I’ve missed. I can of course create my own function to do it.

:rofl: it seems that Enum.find_index is using Enumerable.reduce. I’ll just create my own function that is copy of Enum.find_index that returns both :wink:

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)
2 Likes