- RubyのinjectとPythonのreduce - 偏った言語信者の垂れ流し
- Enumerable#reduceはEnumerable#injectの別名 - http://rubikitch.com/に移転しました
- 畳み込み - fold(l|r), reduce - (hatena (diary ’Nobuhisa))
似たようなことは Smalltalk では #inject:into: を使ってこんなふうに書きます。
(1 to: 10) inject: 0 into: [:result :each | result + each] "=> 55 " (1 to: 10) inject: 1 into: [:result :each | result * each] "=> 3628800 "
ただし、Ruby の #inject と違い、Smalltalk では文法上の制約から第一引数の省略はできません。
一方で、Ruby のブロックと違い Smalltalk のブロックはオブジェクトなので、value: arg1 value: arg2 メッセージに応答できれば(端的には #value:value: メソッドさえ定義されていれば)第二引数のオブジェクトはブロックである必要はありません。
#inject:into: の定義はこんな感じ。
Collection >> inject: thisValue into: binaryBlock | nextValue | nextValue := thisValue. self do: [:each | nextValue := binaryBlock value: nextValue value: each]. ^nextValue