Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a shorthand way to update a specific struct field in racket?

Tags:

scheme

racket

Suppose I have a struct with many fields:

(struct my-struct (f1 f2 f3 f4))

If I am to return a new struct with f2 updated, I have to rephrase every other fields:

(define s (my-struct 1 2 3 4))
(my-struct (my-struct-f1 s)
           (do-something-on (my-struct-f2 s))
           (my-struct-f3 s)
           (my-struct-f4 s))

Which is redundant and would be a source of bugs if I update the number of the fields or changed their orders.

I really wonder if there's a such way I can update a specific field for a struct like:

(my-struct-f2-update (my-struct 1 2 3 4)
                     (lambda (f2) (* f2 2)))
;; => (my-struct 1 4 3 4)

Or I can just set them to a new value as:

(define s (my-struct 1 2 3 4)
(my-struct-f2-set s (* (my-struct-f2 s) 2))
;; => (my-struct 1 4 3 4)

Notice, this is not mutating s here; my-struct-f2-update and my-struct-f2-set should be just returning a copy of s with f2 field updated.

In Haskell I know the 'lens' library that does this job. I'm just wondering if there are some similar ways that I can adopt for racket. Thanks.

like image 451
Shou Ya Avatar asked Feb 19 '15 08:02

Shou Ya


1 Answers

You know what? This is a really good idea. In fact, there have been a few cases in which I wanted this functionality, but I didn't have it. The bad news is that nothing of this sort is provided by Racket. The good news is that Racket has macros!

I present to you define-struct-updaters!

(require (for-syntax racket/list
                     racket/struct-info
                     racket/syntax
                     syntax/parse))

(define-syntax (define-struct-updaters stx)
  (syntax-parse stx
    [(_ name:id)
     ; this gets compile-time information about the struct
     (define struct-info (extract-struct-info (syntax-local-value #'name)))
     ; we can use it to get the constructor, predicate, and accessor functions
     (define/with-syntax make-name (second struct-info))
     (define/with-syntax name? (third struct-info))
     (define accessors (reverse (fourth struct-info)))
     (define/with-syntax (name-field ...) accessors)
     ; we need to generate setter and updater identifiers from the accessors
     ; we also need to figure out where to actually put the new value in the argument list
     (define/with-syntax ([name-field-set name-field-update
                           (name-field-pre ...) (name-field-post ...)]
                          ...)
       (for/list ([accessor (in-list accessors)]
                  [index (in-naturals)])
         (define setter (format-id stx "~a-set" accessor #:source stx))
         (define updater (format-id stx "~a-update" accessor #:source stx))
         (define-values (pre current+post) (split-at accessors index))
         (list setter updater pre (rest current+post))))
     ; now we just need to generate the actual function code
     #'(begin
         (define/contract (name-field-set instance value)
           (-> name? any/c name?)
           (make-name (name-field-pre instance) ...
                      value
                      (name-field-post instance) ...))
         ...
         (define/contract (name-field-update instance updater)
           (-> name? (-> any/c any/c) name?)
           (make-name (name-field-pre instance) ...
                      (updater (name-field instance))
                      (name-field-post instance) ...))
         ...)]))

If you're not familiar with macros, it can look a little intimidating, but it's actually not a complicated macro. Fortunately, you don't need to understand how it works to use it. Here's how you'd do that:

(struct point (x y) #:transparent)
(define-struct-updaters point)

Now you can use all the relevant functional setters and updaters as you'd please.

> (point-x-set (point 1 2) 5)
(point 5 2)
> (point-y-update (point 1 2) add1)
(point 1 3)

I believe there have been some theoretical plans to redesign the Racket struct system, and I think this would be a valuable addition. Until then, feel free to use this solution. I’ve made the code in this answer available as the struct-update package, which can be installed using raco pkg install struct-update.

like image 64
Alexis King Avatar answered Oct 05 '22 03:10

Alexis King