inject:into:

Alexandre Bergel bergel at iam.unibe.ch
Wed Nov 10 21:58:57 UTC 2004


Humm.. It is true that it makes more sense with your explanation.

Thanks,
Alexandre


On Wed, Nov 10, 2004 at 08:42:18AM -0800, Travis Griggs wrote:
> 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.
> 

-- 
_,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:
Alexandre Bergel  http://www.iam.unibe.ch/~bergel
^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;._,.;:~^~:;.



More information about the Squeak-dev mailing list