inject:into:

Travis Griggs tgriggs at key.net
Wed Nov 10 16:42:18 UTC 2004


On Nov 10, 2004, at 0:50, Alexandre Bergel wrote:

> Hi!
>
> I am wondering why the binaryBlock passed to an 'inject:into:' do not 
> bound the current element to the first argument. In some other words 
> why do we write:
>
> #(a b c) inject: '' into: [:partialAnswer :el| partialAnswer , el]
> and not:
> #(a b c) inject: '' into: [:el :partialAnswer| partialAnswer , el]
>
> It would more make sense, at least for me.
> Alexandre

If you view inject:into: as a general purpose accumulator, then the 
current order make some sense. I have shortcut'ed this method with a 
wrapper called accumulate: anAccumulation using: aBlock before. Then 
the classical example:

(1 to: 5) accumulate: 0 using: [:accum :each | accum + each]

or canonically

(1 to: 5) inject: 0 into: [:accum :each | accum + each]

makes more sense. That said... with the addition of fold: into VW, I 
have not used inject:into: in a long while now. fold: is even better 
for the classical example:

(1 to: 5) fold: [:a :b | a + b]

Not only is it terser, but is technically more scalable? Why?

--
Travis Griggs
Objologist
One man's blue plane is another man's pink plane.




More information about the Squeak-dev mailing list