[squeak-dev] A little puzzle

Louis LaBrunda Lou at Keystone-Software.com
Thu May 14 15:54:53 UTC 2015


Hi Stéphane,

I don't have time to work this out right now but how about taking a look at
#findFirst: and seeing if you can replace your #do: with something like
what #findFirst: does.

Lou

>Hello all,
>
>I'm finding myself unable to implement something seemingly simple.  Here 
>is the puzzle:
>
>I want an iterator factory which, given a specific collection, produces 
>a block taking two arguments, also blocks. One (the doBlock) tells what 
>should be done for each element of the collection, and the other (the 
>whileBlock) is a test allowing to abort the whole operation.
>
>So, something like this:
>
>Puzzle>>blockIterating: aCollection
>
>	^ [:doBlock :whileBlock |
>		aCollection do: [:i |
>			(whileBlock value: i) ifFalse: [^ self].
>			doBlock value: i]].
>
>
>Then I could do things like the following:
>
>
>| block |
>
>block := Puzzle new blockIterating: (1 to: 5).
>
>block value: [:p | Transcript show: p; cr] value: [:p | p < 3]
>
>
>	
>But the above fails with a 'Block cannot return' (that's the [^ self] 
>block in the #blockIterating: method). I have attached the code; just do 
>"Puzzle new fail".
>
>I can't find a workaround.
>
>How should I proceed to get a working iterator block ?
>
>Stef
-----------------------------------------------------------
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon
mailto:Lou at Keystone-Software.com http://www.Keystone-Software.com



More information about the Squeak-dev mailing list