> You would know better than I would, but I wonder if you are making an
> effort to bring familiar constructs into Racket that don't really fit,
> instead of looking for more idiomatic ways to do what you want.
>

Yeah, I know what you mean. It's always a struggle to remain both concise,
but at the same time complete enough to convey the point. It's definitely
possible there's a more idiomatic way to do it, but it's not immediately
obvious to me. I have actually given this some thought, which is why I
think exceptions are the best way, but I'll be gladly proven wrong.


> What is it that you're trying to achieve?
>

I want a way to interrupt a generator, which would normally stop it, but at
the same time giving this generator a way to handle the interrupt and
gracefully recover. This pattern of aborting, but giving the possibility to
handle it sounds like a good fit for exceptions to me. The challenge is
that the interruption comes from outside the generator, hence the ability
to raise this exception from outside.

Your suggestion of just passing in a value and letting the generator decide
what to do with it is a possibility I also considered, but it requires
every generator to explicitly handle the possibility of an interruption at
every yield. Jon's way works better, but still requires a slightly
different pattern of wrapping every yield in braces to make sure it calls
the thunk.

Hope that clarifies things a little bit.

cheers,
Kees




>
>
> >
> > On Tue, Feb 19, 2019 at 10:36 PM Jon Zeppieri <zeppi...@gmail.com>
> wrote:
> >>
> >>
> >>
> >> On Tue, Feb 19, 2019 at 9:57 PM Kees-Jochem Wehrmeijer <
> henc...@gmail.com> 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
> >>>
> >>
> >> The most straightforward thing would be to pass the generator a thunk
> that either raises or does not, e.g,
> >>
> >> #lang racket/base
> >>
> >> (require racket/generator)
> >>
> >> (define mygenerator
> >>   (generator (thunk)
> >>              (with-handlers ([exn:fail? (lambda (e) 42)])
> >>                (thunk)
> >>                ((yield 1))
> >>                ((yield 2))
> >>                ((yield 3)))))
> >>
> >> ===
> >> > (mygenerator void)
> >> 1
> >> > (mygenerator (λ () (raise (exn:fail "wah"))))
> >> 42
> >>
> > --
> > 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 racket-users+unsubscr...@googlegroups.com.
> > 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 racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to