[racket] Style or and/define

From: Erik Pearson (erik at adaptations.com)
Date: Tue Jun 11 15:14:47 EDT 2013

On Tue, Jun 11, 2013 at 10:39 AM, Клочков Роман <kalimehtar at mail.ru> wrote:
> You may use srfi/2 http://docs.racket-lang.org/srfi-std/srfi-2.html
> (require srfi/2)
> (define (get-x-spot char-width)
>   (and-let*
>    ([char-width]
>     [dc (get-dc)]
>     [style (or (send (get-style-list) find-named-style "Standard")
>                        (send (get-style-list) find-named-style "Basic"))])
>     (let*-values ([(fnt) (send style get-font)]
>                   [(xw _1 _2 _3) (send dc get-text-extent "x" fnt)])
>            (+ left-padding (* xw char-width))))))

This is cool. It seems to support some idioms very well. Guarding
arguments, fetching some unstable external thing. As long as #f is an
acceptable answer for everything but the correct solution.

In my CL code I would tend to do this through guard expressions which
return early or throw an exception. I find that some of the things
that cause guards to fail are things the user needs to know about,
like misconfigurations, so exceptions are warranted. I also find that
it is clearer to separate the cruft of managing guards from the main
task of the function.

In Racket I still like to separate guards into a distinct conditional,
like if, when, unless. The nesting works well for me. I like a little
bit of it. I would tend to do something like this:

(define (get-x-spot char-width)
  ;; Get my environment into nice short variables.
  (let ([dc (get-dc)]
        [sl (get-style-list)])
    ;; Guard
    (when (and char-width dc sl)
      (let ([style (or (send sl find-named-style "Standard")
                       (send sl find-named-style "Basic"))])
        ;; Another guard
        (when style
          (let*-values ([(fnt) (send style get-font)]
                        [(xw _1 _2 _3) (send dc get-text-extent "x" fnt)])
            (+ left-padding (* xw char-width))))))))


> Вторник, 11 июня 2013, 18:49 +02:00 от Laurent <laurent.orseau at gmail.com>:
> When I see what Robby is forced to write when following the Style:
> https://github.com/plt/racket/commit/09d636c54573522449a6591c805b38f72b6f7da8#L4R963
> I cannot help but think that something is wrong somewhere (it may not be the
> Style, and in case it wasn't clear I'm certainly not criticizing Robby's
> code).
> Using `let' and `and' instead, although being a bit better since it avoids
> all the [else #f], is not that big an improvement:
> (define (get-x-spot char-width)
>   (and
>    char-width
>    (let ([dc (get-dc)])
>      (and
>       dc
>       (let ([style (or (send (get-style-list) find-named-style "Standard")
>                        (send (get-style-list) find-named-style "Basic"))])
>         (and
>          style
>          (let*-values ([(fnt) (send style get-font)]
>                        [(xw _1 _2 _3) (send dc get-text-extent "x" fnt)])
>            (+ left-padding (* xw char-width)))))))))
> Actually I think here the right thing to do might be to allow for internal
> definitions inside `and':
> (define (get-x-spot char-width)
>   (and char-width
>        (define dc (get-dc))
>        dc
>        (define style (or (send (get-style-list) find-named-style "Standard")
>                          (send (get-style-list) find-named-style "Basic")))
>        style
>        (define fnt (send style get-font))
>        (define-values (xw _1 _2 _3) (send dc get-text-extent "x" fnt))
>        (+ left-padding (* xw char-width))))
> Isn't it *much* more readable? (shorter, avoid rightward drift, less parens,
> vertical alignment)
> Since it's not the first time I find the need for such internal definitions
> in `and', maybe this is something to consider for future addition to Racket?
> Or have some people already identified some problems with this idea?
> I've played a bit with it if you want to try by your own:
> https://gist.github.com/Metaxal/5758394
> (not sure I got it all good with syntax-parse though)
> Laurent
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
> --
> Роман Клочков
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Erik Pearson
;; web form and function

Posted on the users mailing list.