Created
August 17, 2011 15:45
-
-
Save danking/1151824 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define-syntax (define-ustruct stx) | |
(define (make-updater-name struct-name field-name) | |
(symbol-append struct-name '-update- field-name)) | |
(define (make-updater-proc name-stx field-stx) | |
(let* ((field-name (syntax->datum field-stx)) | |
(struct-name (syntax->datum name-stx)) | |
(update-proc (datum->syntax name-stx | |
(make-updater-name struct-name | |
field-name)))) | |
#`(define (#,update-proc s val) | |
(struct-copy name s [#,field-stx val])))) | |
(syntax-case stx () | |
((_ name fields) | |
#`(begin (struct name #,@#'fields) | |
#,@(syntax-map (lambda (f) | |
(make-updater-proc #'name f)) | |
#'fields))))) |
Author
danking
commented
Aug 17, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment