I use Enum.count(list) to count the elements and I get the first one with Enum.at(list,0) but the problem is the output. The returned result of Enum.at is: {:a, %{amount: 0, index: 0}}.
I am not sure how I can edit the value of :a , I tried many different ways but couldn’t manage to finish it. For contrast, I would like to set :amount => 2 or some different value.
While this is a list, it might make more sense to work with it as a keyword list. You can use Keyword.update/4 or Keyword.update!/3 to update the value for a key with a function. In this case, your update function would be a map update putting the value 2 for :amount
Even though in a list position matters, its semantically useless in key-value lists. Why assign a key first when you actually identify the items by position?
So I do not think its good practice to do it by index.
It should be roughly doable by the following code though (which is untested):
def change_at(enum, pos, updater) do
{front, [item | tail]} = Enum.split(enum, pos)
front ++ [updater.(item) | tail]
end
This is a quick and dirty hack which should do what you want, but it is not very efficient, also it will crash when the given index is too large.
updater is meant to be a function, which takes the old value of the item and returns the new. My example from above could then look like this (still untested and output is written as I assume it to happen!):
Thats a nice idea, actually to get the key first and then use it to update the KW-list. I do fear though, that the initial input given by the OP is a KW-list only by accident and that a Key-Value-association is not guaranteed.
I do fear though, that the initial input given by the OP is a KW-list only by accident and that a Key-Value-association is not guaranteed.
That’s true. If the “key” in the list is not an atom but still unique, the same thing can be accomplished with List.keyreplace/4. If there is no uniqueness and it has the entries have to be addressed by index, something like this should also work:
I’m sensing an X Y Problem situation - i.e. what is the larger problem that you are actually trying to solve?
You do understand that it is impossible to edit any value? Because all values are immutable, i.e. no value can be changed.
So the best you can do is to create an entirely new list which includes the modifications that you want so that you can use the new list instead of the old list (which is what the answers shown so far are doing).