I'm not sure if this is exactly what you want, but the handy module (which
I still need to split up into less of a Fibber McGee) includes handy/try.
This would let you do the following:
#lang racket
(require handy/try)
(define x (random 100))
; these are obviously silly functions that are only for the sake of example
(define (fails-first-check? x) (= 0 (modulo x 10)))
(define (fails-second-check? x) (= 0 (modulo x 7)))
(define (calculate-final-result z) 'final-result)
(displayln
(try [(displayln (~a "x is: " x))
(when (fails-first-check? x) (raise 'failed-first-check))
(when (fails-second-check? x) (raise 'failed-second-check))
(define z 'z)
; ...do something with z
(calculate-final-result z)]
[catch
((curry equal? 'failed-first-check) (lambda (e) "failure #1"))
((curry equal? 'failed-second-check) (lambda (e) "failure #2"))
(any/c (lambda (e)
'last-chance-processing-here))]))
It also supports pre and post checks:
#lang racket
(require handy/try)
(define x (random 100))
; real code would do something more sensible
(define (fails-first-check? x) (= 0 (modulo x 10)))
(define (fails-second-check? x) (= 0 (modulo x 7)))
(define (calculate-final-result z) 'final-result)
(displayln
(try [pre (displayln "pre checks are guaranteed to happen")]
[(displayln (~a "x is: " x))
(when (fails-first-check? x) (raise 'failed-first-check))
(when (fails-second-check? x) (raise 'failed-second-check))
(define z 'z)
; ...do something with z
(calculate-final-result z)]
[catch
((curry equal? 'failed-first-check) (lambda (e) "failure #1"))
((curry equal? 'failed-second-check) (lambda (e) "failure #2"))
(any/c (lambda (e)
'last-chance-processing-here))]
[finally
(displayln "do final cleanup here -- delete temp files, close
sockets, etc")
(displayln "this is guaranteed to run even if an uncaught exception
is raised")
]))
The main body is required but 'pre', 'catch', and 'finally' are all
optional. The whole thing is a dynamic-wind wrapped around a with-handlers
so pre and finally are guaranteed to execute no matter what. pre is out of
scope for the main body which is suboptimal but if there was interest then
I might see about fixing that.
Unfortunately, like many of the submodules in handy 'try' has extensive
documentation in the comments but not in Scribble. Again, I should get
around to that in my Copious Free Time. For now, it's here:
https://github.com/dstorrs/racket-dstorrs-libs/blob/master/try.rkt
On Wed, Oct 28, 2020 at 9:48 AM Hendrik Boom <[email protected]> wrote:
> On Wed, Oct 28, 2020 at 03:54:29AM -0700, Jack Firth wrote:
> > So I'm a little tired of writing code like this:
> >
> > (define x ...)
> > (cond
> > [(take-shortcut? x) (shortcut x)]
> > [else
> > (define y (compute-y x))
> > (cond
> > [(take-other-shortcut? x y) (other-shortcut x y)]
> > [else
> > (define z ...)
> > (cond ...)])])
>
> Perhaps you could use parendown
> https://docs.racket-lang.org/parendown/index.html
>
> #lang parendown racket/base
> (define x ...)
> (cond
> [(take-shortcut? x) (shortcut x)]
> #/ else
> (define y (compute-y x))
> #/ cond
> [(take-other-shortcut? x y) (other-shortcut x y)]
> #/ else
> (define z ...)
> #/ cond ...
> )
>
> >
> > That is,
> Frequently the lase element of a list is another list, and a large one
> at that.
> Using #/ makes that a kind of tail-recursive syntax and eliminates some
> explicit parentheses.
>
> Of course when you start using this it becomes so common that you'd like
> to drop the ugly #'s, but unfortunately, / is already taken.
>
> -- hendrik
>
> --
> 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].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/20201028134830.54plnv6nv5j3lcmt%40topoi.pooq.com
> .
>
--
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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/racket-users/CAE8gKodfdWieNE2iYqOO-Jajqv3E-%2B8MUhV-%2BwhPkShUvYouXA%40mail.gmail.com.