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

Jaromir Matas m at jaromir.net
Mon Mar 8 20:38:51 UTC 2021


> 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


More information about the Squeak-dev mailing list