Pipe syntax and the current methods

Andrew Tween amtween at hotmail.com
Mon Aug 27 20:26:10 UTC 2007


"Randal L. Schwartz" <merlyn at stonehenge.com> wrote in message 
news:86y7fwalgn.fsf at blue.stonehenge.com...
>
> Would any of the "pipe" advocates mind taking a stab at the *current* 
> source
> methods, and rewrite the method showing how pipe syntax would have 
> simplified
> or clarified the method?

I wouldn't describe myself as an 'advocate', but I don't mind exploring the 
concept.
So here is an example..

Collection has a helper method #select:thenCollect:  .
Looking at the senders of that, I found ChangeSet 
class>>highestNumberedChangeSet
(I have reformatted the source to make things clearer)

   highestNumberedChangeSet
      "ChangeSorter highestNumberedChangeSet"
     | aList |
     aList := self allChangeSetNames
        select: [:aString | aString startsWithDigit]
        thenCollect: [:aString | aString initialIntegerOrNil].
     ^aList size > 0
        ifTrue: [aList max]
        ifFalse: [nil]

With pipes, this could be written as

    highestNumberedChangeSet
        "ChangeSorter highestNumberedChangeSet"
        ^self allChangeSetNames
            select:[:aString | aString startsWithDigit]  ;;
            collect:[:aString | aString initialIntegerOrNil] ;;
            ifNotEmpty:[:list | list max]

The problem with these kind of helper methods is that there many other 
permutations (collect:thenDo:, collect:thenSelect: select:thenDo: 
select:thenCollect:thenDetect: etc.).
Either, methods are created for all permutations, and most of them never 
actually used. (Collection>>collect:thenDo: has no senders).
Or, only those with senders are added, and the question is asked, for 
exanple, :-  Why is there a #select:thenDo: but no #collect:thenDo: ?

With the pipe you get all permutations, without needing to create methods 
for them all.

Here is another example; a snippet from Morph>>renameTo:

  classes := (self systemNavigation allCallsOn: assoc)
    collect: [:each | each classSymbol].
  classes asSet
    do: [:clsName | (Smalltalk at: clsName) replaceSilently: oldName to: 
aName].

which, with pipes, could be rewritten as...

  self systemNavigation
    allCallsOn: assoc ;;
    collect: [:each | each classSymbol] ;;
    asSet ;;
    do: [:clsName | (Smalltalk at: clsName) replaceSilently: oldName to: 
aName].

Cheers,
Andy 





More information about the Squeak-dev mailing list