[squeak-dev] A little puzzle
Stéphane Rollandin
lecteur at zogotounga.net
Thu May 14 15:33:45 UTC 2015
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
-------------- next part --------------
'From Squeak4.5 of 10 May 2015 [latest update: #15009] on 14 May 2015 at 4:24:25 pm'!
Object subclass: #Puzzle
instanceVariableNames: 'block'
classVariableNames: ''
poolDictionaries: ''
category: 'Bugs'!
!Puzzle commentStamp: '<historical>' prior: 0!
Puzzle new fail "doIt"!
]style[(15 7)cblack;,!
!Puzzle methodsFor: 'as yet unclassified' stamp: 'spfa 5/14/2015 16:22'!
blockIterating: aCollection
^ [:doBlock :whileBlock |
aCollection do: [:i |
(whileBlock value: i) ifFalse: [^ self].
doBlock value: i]].! !
!Puzzle methodsFor: 'as yet unclassified' stamp: 'spfa 5/14/2015 16:22'!
fail
block := self blockIterating: (1 to: 5).
block
value: [:p | Transcript show: p; cr]
value: [:p | p < 3]
! !
More information about the Squeak-dev
mailing list
|