really silly question

Marcel Weiher marcel at metaobject.com
Mon Sep 3 15:33:29 UTC 2001


On Sunday, September 2, 2001, at 10:19 PM, Lex Spoon wrote:

[snip]
> Finally, I'll add my personal experience: I still find it tricky to add
> a *new* kind of loop to my code.  Now, I can whip out a "iterate over
> the elements and accumulate into a subsidiary array" very quickly, but
> that's because I've seen this kind of loop a zillion times!  If the loop
> has a structure that's new, I really have to think about it for a minute
> or two, even after 15-20 years of programming (depending on how you
> count).  It just so turns out that almost all loops are familiar ones,
> so this isn't an issue for me in practice.  Perhaps other programmers
> will introspect the same thing?

Yes, very much so.  However, since almost all loops are 
familiar/similar, it seems one shouldn't have to write them...

> Anyway, now for the good news: Smalltalk allows you to get away from
> low-level loop algorithms in almost all cases that occur in practice.

...which is why it is good when one doesn't have to.

> The reason Smalltlak can do this, and many languages can't, is because
> of the powerful combination of collections and blocks.

You can actually do it just as well using just messaging, with Higher 
Order Messaging (HOM).  I personally find blocks also to be somewhat 
obscure.

>   Consider this
> wonderful response to the original question:
>
> 	firstArray with: secondArray collect: [ :a :b | a + b ]
>
> Is this really a loop?  Technically, with:collect: is using a loop
> internally, but cognitively, it just doesn't seem the same to me as a
> hand-coded loop.

I like

	firstArray collect + secondArray each

better, because it is even further away from the single elements and has 
even less goobledygook.

>  All the details of incrementing an index and moving
> toward completion are left out, and it just looks like combining two
> collections with an operation.  Smushing two collections together really
> seems simpler to me than "start with element 1; do stuff with element i;
> move to element i+1;" etc.

Exactly!  I find it even easier when it is all just done with 
messaging.  I've been into this far too long to be unbiased, but it 
seems to me that "this message gets sent to lots of objects instead of 
just one object" is easer than that of a block being invoked, especially 
since the single elements don't even show up any longer.

>
> By the way, there is a nice generalization of the above with:foo:
> pattern.  It looks like this:
>
> 	(firstArray with: secondArray) collect: [ :combo | combo first + combo
> second ]

I have to admit I like

	firstArray collect + secondArray each

better, though it is far from perfect, because the structure of the 
intended is still visible in the lexical structure of the code (LHS  +  
RHS), even if both the LHS and the RHS are a little mucked up.

[snip description of with: ->  it does seem useful]

> Overall, I'm really starting to think loops should be treated like
> assembly language or two's complement: interesting for the
> terminally curious, probably useful to know a *little* about, but not
> incredibly useful in most programs.

That's been my thinking exactly for a couple of years.  As a matter of 
fact, the same goes for many of the other "patterns" of code we keep 
producing.  As soon as we have another of these patterns, we should be 
able to put it away and just use it, instead of re-creating it.  
However, in order for this to happen, or means of expressing and reusing 
such patterns must improve, the cost of putting them away and accessing 
them must decrease.

>  (Of course, this is only true in a
> nice language that actually has collections and blocks!)

Or even ones without blocks, as long as they have true messaging!

> John, you're not at all alone.  :)

Yup!

Marcel





More information about the Squeak-dev mailing list