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
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
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)
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.
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.
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.
For those wanting a use case. A contrived use case
You have 1 CSV row and you know it’s in sync with the headers which you also have but you don’t have a guarantee on the header order across report generations (I know this is a bit contrived but it’s the reality of the stupid system we’re exporting these CSVs from which we don’t have much control over).
So you need to figure out which index the header is at then fetch the corresponding index within the CSV row that you have.
Sounds like you’d do better keeping rows in maps keyed by header and then using the order of headers in the final report to turn your row maps into a list of values.