Hacker News new | past | comments | ask | show | jobs | submit login
A bit of learning about Racket structures (grinning-cat.com)
5 points by robert_postill 7 months ago | hide | past | favorite | 2 comments



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

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....

[2]: https://docs.racket-lang.org/lens/index.html


Thanks for the tip, I'll check it out.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: