Dispatching blocks to another context and break for collections

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Dispatching blocks to another context and break for collections

Holger Freyther
Hi Paolo,

I am searching for a simple pattern and wonder if you or anyone else
encountered this and came up with a nice solution.

I have a SortedCollection (of objects that have a timestamp), I want to
process the timestamps until a certain date, I want to avoid extra copies.

Right now I use code like this:

copy do: [:each |
        each isTooOld ifTrue: [^true].
        each doStuff
        orig remove: each
].


Now I started to dispatch the block to another process (to avoid some locking
issues right now..) but obviously I get a BadReturn when invoking it in
another Context. Is there something like a 'break' for Iterable>>do:? is there
any other selector to help me?

One thing I can think of is something like this...

[copy isEmpty not and: [copy first isJounEnough]] whileTrue: [].

anything else?


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: Dispatching blocks to another context and break for collections

Paolo Bonzini-2
On 06/28/2011 07:49 PM, Holger Hans Peter Freyther wrote:

> copy do: [:each |
> each isTooOld ifTrue: [^true].
> each doStuff
> orig remove: each
> ].
>
>
> Now I started to dispatch the block to another process (to avoid some locking
> issues right now..) but obviously I get a BadReturn when invoking it in
> another Context. Is there something like a 'break' for Iterable>>do:? is there
> any other selector to help me?

There's of course exception handling, but that's pretty heavyweight.
I've seen people use this:

Iterable>>withBreakDo: aBlock
     break := [ :value | ^self].
     self do: [ :each | aBlock value: each value: break].

so that you can do

copy do: [ :each |
     each isTooOld ifTrue: [break value: true].
     each doStuff
     orig remove: each ]

> One thing I can think of is something like this...
>
> [copy isEmpty not and: [copy first isJounEnough]] whileTrue: [].
>
> anything else?

This one definitely has better performance.

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk