(newbie) String / Array / ArrayedCollection

nicolas cellier ncellier at ifrance.com
Sun Feb 5 03:57:04 UTC 2006


Hello all,
I bounce on your example because i feel more concerned about arithmetic.

Primary interest of implementing a #sum #product #min #max #cumulativeSum 
etc.. method in collection of numbers is the possibility to optimize some of 
these messages at the VM level. That's getting important when you have 
millions of numbers to crunch...

Suppose now you want the sum of squares:

 (aCollection collect: [:element | element squared]) sum.

Of course better code it in a single loop:

 aCollection inject: 0 into: [:sum :element | sum + element squared].

It does also work with fold: i presume.
But i personnally prefer the more legible:

 aCollection sum: [:element | element squared]

or maybe:

 aCollection sumOf: [:each | each squared]

You can also have the productOf: minOf: maxOf: cumulativeSumOf: 
cumulativeProductOf: and get something powerfull...

If and only if you have optimized primitives, you'd better operate on 
collections and have no loop at all in Smallatlk (at the expense of a memory 
allocation and two loops in C code however), this is exactly how matlab 
works:

 (aCollection*aCollection) sum

In my opinion, messages without a block argument are necessary for 
optimization purposes in a matlab like environment.
Messages with block arguments, though not strictly necessary knowing fold: and 
inject:into:, would ease implementation and code understanding of matrices 
and polynomials for example. To me they seem obvious extensions.

I already proposed all that to VisualWorks NumericalCollections package.
(Cincom Public Store).

I also proposed the #count: extension.
 aCollection count: [:each | each > 1]
does answer the number of elements satisfying a condition (I feel 
countSatisfy: is a little heavy). That is usefull more than rarely i think.

What is your opinion ?




More information about the Squeak-dev mailing list