How about this?:


On Tue, Feb 19, 2019, 9:57 PM Kees-Jochem Wehrmeijer <[email protected]>
wrote:

> Basically when I call the function throw. So e.g.
>
> (define mygenerator (generator ()
>                                (with-handlers ([exn:fail? (lambda (e) 42)])
>                                  (yield 1)
>                                  (yield 2)
>                                  (yield 3))))
>
> > (mygenerator)
> 1
> > (throw mygenerator)
> 42
>

(define (mygenerator [throw? #f])
 (if throw? 42 (begin (yield 1)(yield 2)(yield 3)))

> (mygenerator)
1
> (mygenerator 'throw)
42

What sort of thing would you use this technique for? You aren't actually
throwing anything, because you trap and discard the exception before
returning a value normally.



> On Tue, Feb 19, 2019 at 6:20 PM David Storrs <[email protected]>
> wrote:
>
>> Under what circumstances would you want it to throw? When the generator
>> runs out, or...?
>>
>> On Tue, Feb 19, 2019, 3:59 PM Kees-Jochem Wehrmeijer <[email protected]>
>> wrote:
>>
>>> Hi,
>>>
>>> Python allows to call a .throw() method on a generator. I was wondering
>>> if Racket generators have a similar feature. From the docs it doesn't seem
>>> to have that. One way I could see around this is to set a parameter and
>>> then check for that parameter in the generator and raise an exception based
>>> on that. I could imagine writing a macro, e.g. yield-except, that does that
>>> check. The disadvantage would be that users would have to remember to use
>>> yield-except instead of regular yield in the generator.
>>>
>>> Are there any other clever workarounds that I'm missing?
>>>
>>> Thanks,
>>> Kees
>>>
>>> --
>>> You received this message because you are subscribed to the Google
>>> Groups "Racket Users" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to