Just abstract the common part out? E.g.,

(define (update-text-field read-field write-field converter)
  (define text (send read-field get-value))
  (cond
    [(string=? text "")
     (send read-field set-field-background (make-object color% 255 255 255 1))
     (send write-field set-value "")]
    [(number? (string->number text))
     (send read-field set-field-background (make-object color% 255 255 255 1))
     (send write-field set-value (converter (string->number text)))]
    [else
     (send read-field set-field-background (make-object color% 255 0 0 1))
     (send write-field set-value "")]))

(define (update-fahrenheit control event)
  (update-text-field text-celsius text-fahrenheit convert-c))

(define (update-celsius control event)
  (update-text-field text-fahrenheit text-celsius convert-f))

Note that your original code has a bug: you always use convert-c in both
directions. Obviously, in update-celsius you want to use convert-f.

There’s also another subtle bug that I didn’t fix. Suppose you type an
invalid character (like “a”) to the left text field. This will make the
field become red. Now, switch to the right field and type “32”. The left
field will output “0” while being red, though it probably should now be
white.

On Sat, May 25, 2019 at 7:57 PM Travis Hinkelman <travis.hinkel...@gmail.com>
wrote:

> Hi All,
>
> I'm working through the tasks in 7guis
> <https://eugenkiss.github.io/7guis/tasks> to learn about GUI programming
> in Racket. My code
> <https://gist.github.com/hinkelman/e35d4abf3203277c4428bcd6abad8183> for
> the temperature converter task works as expected but there is a lot of
> redundancy in my update-fahrenheit and update-celsius callback functions. I
> would appreciate feedback on how to make this code less redundant.
>
> Thanks,
>
> Travis
>
> --
> 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.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/racket-users/8f97260c-62be-4a6f-9459-244a88632ef9%40googlegroups.com
> <https://groups.google.com/d/msgid/racket-users/8f97260c-62be-4a6f-9459-244a88632ef9%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
> 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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/racket-users/CADcuegukbchny44MGo6cnDH8THEfp74t09%2B0sqZTPa3UNiOa2w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to