[squeak-dev] Collection's #fold: vs #reduce:
juan at jvuletich.org
Tue Nov 2 20:23:42 UTC 2010
Nicolas Cellier wrote:
> I'd say which selector are implemented in Pharo and Cuis ?
> We should better converge.
Cuis doesn't include any of them yet. I can add whatever people prefer.
What I'd do different is the implementation:
"Evaluate the block with the first two elements of the receiver,
then with the result of the first evaluation and the next element,
and so on. Answer the result of the final evaluation. If the receiver
is empty, raise an error. If the receiver has a single element, answer
#('if' 'it' 'is' 'to' 'be' 'it' 'is' 'up' 'to' 'me') fold: [:a :b |
a, ' ', b]
| noPreviousValue |
noPreviousValue := Object new. "something that can't be in the
^self inject: noPreviousValue into: [ :previousValue :each |
previousValue == noPreviousValue
ifTrue: [ each ]
ifFalse: [ aBinaryBlock value: previousValue value: each ]]
This is easier to understand, and it also makes clear the relation
between #fold: (or #reduce:) and #inject:into: .
More information about the Squeak-dev