So, from what I know, we can only update one value using Enum.reduce/3
. Is it possible to update multiple values using Enum.reduce/3
without using maps?
To generalize this in more coding terms, I want to update the values of both mycurr
and myglobal
. Now if we use Enum.reduce/3
, we cannot update both of these without incorporating them in a map and passing that off as an accumulator. The reason why I don’t want to use maps here is because that would increase the space complexity to O(n).
An example of what I want to do in Python is like this:
mylist = [1,2,3,4,5]
mycurr = mylist[0]
myglobal = mylist[0]
for val in mylist:
if mycurr < 0:
mycurr = val
else:
mycurr += val
if myglobal < mycurr:
myglobal = mycurr
Over here, both mycurr
and myglobal
gets updated. In Elixir, if we try using Enum.reduce/3
, we can only update one of this unless we use a map as an accumulator, which would increase the space complexity to O(n).
Enum.reduce(mylist, 0, fn(val, acc) -> # 0 here refers to acc which is one one of mycurr or myglobal
Are there any better alternatives to using Enum.reduce/3
for this or is it possible to update multiple values without using a map as an accumulator?