[squeak-dev] Collections/Streams | About enumerating a sequence up to a matching query ...

Marcel Taeumel marcel.taeumel at hpi.de
Tue Mar 9 08:40:38 UTC 2021


Hi all.

I like the use of "upThrough" vs. "upTo" to distinguish inclusive from exclusive. Like the existing #copyUpThrough:. (So, not "until".)

I think that #readStream should not be used in any collection's method (implementation). Instead, we now about #size and sometimes #array, so we should use #do: and similar. Or at least *try* not use it. Yes, I know about #streamContents:, which I think is really valuable.

Even if we would add something like #collect:upThrough: or #collect:upTo:, I would not want to change the implementation of #collect:. Maybe use #collect: to implement the new interface, but not change it. An [:each | false] would just involve too many redundant checks.

Considering the order of predicates, I would just support first-order predicates for now because that's what our most recent use cases would need. :-)

***

All in all, I would suggest to just add #do:upThrough: and #do:upTo: to Sequenceable collection as well as #upThrough: to (Positionable)Stream. Let's not over-engineer this for now but see where such increment leads us.

Then, my example use from the beginning could look like this:

Array streamContents: [:result |
   aButton withAllOwners
      do: [:morph | result nextPut: morph color]
      upThrough: [:morph | morph isSystemWindow]].

Maybe, at a later point, we could evaluate the need for extending #reject:, #select:, #collect:, too. Maybe.

What do you think?

Best,
Marcel
Am 09.03.2021 08:07:23 schrieb Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com>:
Also the question of inclusion will arise as well as the order of predicates...

There could also be symetrical once: [:x | x > 10].

Le lun. 8 mars 2021 à 23:33, Nicolas Cellier <nicolas.cellier.aka.nice at gmail.com [mailto:nicolas.cellier.aka.nice at gmail.com]> a écrit :

I like that, it's beginning to be expressive, even better than clojure. However, i think that clojure offers composable predicates thanks to lazyness... like Xtreams.

Le lun. 8 mars 2021 à 21:38, Jaromir Matas <m at jaromir.net [mailto:m at jaromir.net]> a écrit :

> I am looking for something like #collect:until: or #upToSatisfying:. I do
want the stop element to be included here, not sure about the general case.
>
> aButton withAllOwners
>    collect: [:morph | morph color]
>    until: [:morph | morph isSystemWindow].
>

Hi again, ahh so you're actually looking for a generalized collect for any
SequencableCollection or Stream :) Like this?

(1 to: 100)
        collect: [:x | x squared]
        where: [:x | x even]
        until: [:x | x squared = 2500]


collect: collectBlock where: whereBlock until: untilBlock

        | result supplier |
        supplier := self readStream.
        result := {} writeStream.
        [[supplier atEnd]
            whileFalse: [ | val |
              val := supplier next.
              (whereBlock value: val) ifTrue: [result nextPut: (collectBlock value:
val)].
              (untilBlock value: val) ifTrue: [^result contents]]
        ] value.
        ^result contents

collect: colBlock until: untilBlock

        ^self collect: colBlock where: [:each | true] until: untilBlock

or even:

collect: colBlock

        ^self collect: colBlock until: [:each | true]




-----
^[^ Jaromir
--
Sent from: http://forum.world.st/Squeak-Dev-f45488.html [http://forum.world.st/Squeak-Dev-f45488.html]

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20210309/c1a90372/attachment.html>


More information about the Squeak-dev mailing list