Given some list of arbitrary precision numbers, e.g. (actual lists are 1M+ elements long):
test={0, 2, 2, 47839283, 2, 0, 0, 2, 0, 1, 2, 0}
I need to accumulate the list, but where a specified element value is treated as "clear", that is, the accumulation is reset to zero at that point in the list and continues.
With the above example, using zero as the "clear", the result would be e.g.:
{0, 2, 4, 47839287, 47839289, 0, 0, 2, 0, 1, 3, 0}
I'm using
Function[{lst, clr}, FoldList[If[#2 == clr, 0, +##] &, lst[[1]], Rest@lst]][test, 0]
Seems pretty snappy, wondering if there's a more efficient way of doing this.
Comments
Post a Comment