Is something like #collect:until: exist?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is something like #collect:until: exist?

Mathieu SUEN
Thanks
        Math

Reply | Threaded
Open this post in threaded view
|

re: Is something like #collect:until: exist?

Schwab,Wilhelm K
Is this what you had in mind?

collect: aBlock until:quitBlock
        "Evaluate aBlock with each of the receiver's elements as the
argument.
        Stop when quitBlock evaluates to true.
        Collect the resulting values into a collection like the
receiver. Answer  
        the new collection."

        | newCollection |
        newCollection := self species new.
        self do: [:each |
                ( quitBlock value:each )
                        ifFalse:[
                                newCollection add: (aBlock value: each)
                        ]
                        ifTrue:[
                                ^newCollection.
                        ].
        ].
        ^ newCollection.


You might first look at #select:thenCollect: to see if it meets your
needs.  The "until" is a little stream-like, so you might also consider
moving items from a read stream to a write stream on a new collection
and then collect from it.

Bill




Wilhelm K. Schwab, Ph.D.
University of Florida
Department of Anesthesiology
PO Box 100254
Gainesville, FL 32610-0254

Email: [hidden email]
Tel: (352) 846-1285
FAX: (352) 392-7029


Reply | Threaded
Open this post in threaded view
|

Re: Is something like #collect:until: exist?

Mathieu SUEN
Bill Schwab a écrit :

> Is this what you had in mind?
>
> collect: aBlock until:quitBlock
> "Evaluate aBlock with each of the receiver's elements as the
> argument.
> Stop when quitBlock evaluates to true.
> Collect the resulting values into a collection like the
> receiver. Answer  
> the new collection."
>
> | newCollection |
> newCollection := self species new.
> self do: [:each |
> ( quitBlock value:each )
> ifFalse:[
> newCollection add: (aBlock value: each)
> ]
> ifTrue:[
> ^newCollection.
> ].
> ].
> ^ newCollection.
>
>
> You might first look at #select:thenCollect: to see if it meets your
> needs.  The "until" is a little stream-like, so you might also consider
> moving items from a read stream to a write stream on a new collection
> and then collect from it.
>
> Bill
>

Yes it's what mean your script I was just wanted to know if it was already in the image.
And it's also to manipulate a stream.
Thanks

>
>
>
> Wilhelm K. Schwab, Ph.D.
> University of Florida
> Department of Anesthesiology
> PO Box 100254
> Gainesville, FL 32610-0254
>
> Email: [hidden email]
> Tel: (352) 846-1285
> FAX: (352) 392-7029
>
>
>