定義。
SequenceableCollection >> reducel: binaryBlock ^self allButFirst inject: self first into: binaryBlock
SequenceableCollection >> reducer: binaryBlock | nextValue | nextValue := self last. self allButLast reverseDo: [:each | nextValue := binaryBlock value: each value: nextValue]. ^nextValue
使用例。
(1 to: 4) reducel: [:x :y | '({1}#{2})' format: {x. y}] "=> '(((1#2)#3)#4)' "
(1 to: 4) reducer: [:x :y | '({1}#{2})' format: {x. y}] "=> '(1#(2#(3#4)))' "
#reducer: は、あくまで #inject:into: を使って、こんなのでも。
reducer: binaryBlock ^self allButLast reverse inject: self last into: [:a :b | binaryBlock value: b value: a]