How does push!() work within accumulate()?

why are the two formulations of accumulate() different?


accumulate((s,c)->push!(s,c),1:3,init=[]) != accumulate((s,c)->[s;c],1:3,init=[]) #true

push!([1,2],3)==[[1,2];3]  # true

push! mutates a vector, while concatenation with [s; c] creates a completely new vector. In the first case, you’re mutating that same initial vector [] over and over, so each element in the result is the same vector and has the same value. In the second case, you’re creating a brand new vector with [s; c] at each iteration, so the accumulated results aren’t all identical.

1 Like