[squeak-dev] Collections/Streams | About enumerating a sequence up to a matching query ...
Thiede, Christoph
Christoph.Thiede at student.hpi.uni-potsdam.de
Tue Mar 9 12:23:06 UTC 2021
Hi Marcel,
I would pay additional attention to the order of arguments. :-)
#do:upThrough: reads to me as: First do something for every element, then select all elements (which elements? the original elements or a collected copy?) as long as they do not meet a requirement. Which probably would not be the idea of this selector since it would be unnecessarily slow, wouldn't it?
What would (1 to: 10) do: [:ea | self inform: ea] upThrough: [:ea | false] do? Show zero, one, or ten dialogs?
In the following assuming that the right answer is zero ...:
I think #selectUpThrough:thenDo: and #selectUpThrough:thenCollect: might fit better. Or maybe #takeUpThrough:... instead. It communicates the order of block execution more transparently and would be consistent and colocated to the existing #select:then[Do|Collect]: methods.
What do you think? :-)
Best,
Christoph
<http://www.hpi.de/>
________________________________
Von: Squeak-dev <squeak-dev-bounces at lists.squeakfoundation.org> im Auftrag von Taeumel, Marcel
Gesendet: Dienstag, 9. März 2021 09:40:38
An: squeak-dev
Betreff: Re: [squeak-dev] Collections/Streams | About enumerating a sequence up to a matching query ...
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20210309/ce3b1db1/attachment.html>
More information about the Squeak-dev
mailing list
|