[squeak-dev] A little puzzle

Balázs Kósi kosi.balazs at ezomer.hu
Thu May 14 16:07:00 UTC 2015


Hi!

If your collection understands #readStream, you may write:

Puzzle >> blockIterating: aCollection

    ^[ :doBlock :whileBlock |
        | stream item |
        stream := aCollection readStream.
        [ stream atEnd not and: [
            whileBlock value: (item := stream next) ] ] whileTrue: [
                 doBlock value: item ] ]

If it only understands #do:, you may wrap it in a Generator:

Puzzle >> blockIterating: aCollection

    ^[ :doBlock :whileBlock |
        | stream item |
        stream := Generator on: [ :g |
            aCollection do: [ :each | g nextPut: each ] ].
        [ stream atEnd not and: [
            whileBlock value: (item := stream next) ] ] whileTrue: [
                 doBlock value: item ] ]

If it's not a requirement to return a Block, you may create a class,
whose instances respond to #value:value: and sidestep the problem.

Cheers, Balázs
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/squeak-dev/attachments/20150514/2398f3ea/attachment.htm


More information about the Squeak-dev mailing list