3 ms·
A couple ergonomic things, you can use struct-out[1] to give access to the generated forms: (provide (struct-out examined-row)) Lenses[2] can simplify funct
by iamevn 5y ago
A couple ergonomic things, you can use struct-out[1] to give access to the generated forms:
(provide (struct-out examined-row))
Lenses[2] can simplify functionally "updating" nested data structures. To recreate the example with a lens:
(provide (struct+lens-out time-card))
(struct/lens time-card (start-time end-time))
; define card and finish-time the same way
(lens-set time-card-end-time-lens card finish-time)
This is not any better than struct-copy for a simple use but with nested data structures it simplifies things quite a bit imo:
; maybe you have a hash mapping employee IDs to time-cards
(define (clock-out daily-timecards employee time)
(lens-set (lens-compose time-card-end-time-lens
(hash-ref-lens employee))
; alternatively lens-thrush evaluates the other way
; (#; comments out an s-expression)
#;(lens-thrush (hash-ref-lens employee)
time-card-end-time-lens)
daily-timecards time))
another example (probably with better formatting since I typed this on a phone) at the bottom of https://docs.racket-lang.org/lens/struct-guide.html https://docs.racket-lang.org/lens/struct-guide.html
The guide for lenses in this documentation is really good imo, I've even seen haskellers point other haskellers to it to learn how to use lenses.
[1]: https://docs.racket-lang.org/reference/require.html#%28form._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._struct-out%29%29 https://docs.racket-lang.org/reference/require.html#%28form....
[2]: https://docs.racket-lang.org/lens/index.html https://docs.racket-lang.org/lens/index.html
- robert_postill 5y agoThanks for the tip, I'll check it out.