Yogesh Gosavi
11/30/2018, 4:27 PMhudsonb
11/30/2018, 4:30 PMfold
is much more general. It allows you to choose what the type of the accumulator is and its initial valuehudsonb
11/30/2018, 4:31 PMreduce
accumulator type is fixed, it is always the same type as the element type.hudsonb
11/30/2018, 4:32 PMIntArray
, you could use reduce
because you are reducing to the same type (Int
)hudsonb
11/30/2018, 4:33 PMIntArray
, you would have to use fold
cause you are reducing to a Map<Int, Int>
(or similar structure) and not just simply an Int
Yogesh Gosavi
11/30/2018, 4:41 PMShawn
11/30/2018, 5:28 PMhudsonb
11/30/2018, 5:29 PMfold
, you may use any type you want for an accumulator. That's not true in reduce
, the accumulator type is fixed.nekomatic
11/30/2018, 5:31 PMfold
relays on a accumulator, its execution is sequential, reduce
relays on the concept of identity and associativity and does not require operations to be executed in any particular ordernekomatic
11/30/2018, 5:36 PMfold
scans through your collection and appends each value to the accumulator, reduce
picks any two adjacent values and applies the operation >>replacing<< (but not really) the values with the result then repeats. The difference is that reduce
can be done in parallel, fold
not really.nekomatic
11/30/2018, 5:39 PMfold
is a line of operations, reduce
is a tree