On 04/24/2017 11:16 AM, Esteban A. Maringolo wrote:> For these use cases it would be nice to have some sort of syntax sugar
as with the cascade operator, but with a chaining operator instead.

This doesn't even require syntactic sugar, just objects, and this is a more general problem even with a back to back chain of select/detect/reject style things. I solved this for myself years ago by introducing what I call a pipe (nod to unix) allowing me to chain calls in a pipeline without all the parens with each call acting on the result value of the last call. Trivially implemented using #doesNotUnderstand: and the cascade operator.

        ^ dict1 asPipe at: 'key1'; at: 'key2'; at: 'key3'.

Anytime I have a bunch of back to back calls that would require a lot of parens, I just create a pipe...

maxRoomsAvailable
        ^self findBlocks asPipe
select: [ :e | e blockDate between: actualCheckInDate and: actualCheckInDate + (nights - 1) days ];
                detectMin: [ :e | e quantityAvailable ];
                quantityAvailable

The simplest implementation is...

Object>>asPipe
        ^ SPipe with: self

Object subclass: #SPipe
    instanceVariableNames: 'value'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'PharoExtensions'

privateValue: anObject
    value := anObject

yourself
        ^ value yourself

doesNotUnderstand: aMessage
^ value := value perform: aMessage selector withArguments: aMessage arguments

SPipe class>>with: anObject
    ^ (self new)
        privateValue: anObject;
        yourself



--
Ramon Leon

Reply via email to