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.
--
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...@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.
For more options, visit https://groups.google.com/d/optout.
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, inupdate-celsius
you want to useconvert-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....@gmail.com> wrote:
Hi All,--I'm working through the tasks in 7guis to learn about GUI programming in Racket. My code 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...@googlegroups.com.