Paste number 135858: wtih

Paste number 135858: wtih
Pasted by: dobrocoder
When:8 years, 2 months ago
Share:Tweet this! |
Paste contents:
Raw Source | XML | Display As
#lang racket
(require (for-syntax racket/syntax))

(define-syntax (struct/with stx)
  (syntax-case stx ()
    [(_ struct-name fields) 
     (with-syntax ([with-struct-name (format-id stx "~a-~a" "with" #'struct-name)]
                   [field-names (datum->syntax stx (map (λ (x) (if (pair? x) (car x) x)) (syntax->datum #'fields)))])
           (struct struct-name fields)
           (define-syntax (with-struct-name with-stx)
             (syntax-case with-stx ()
               [(_ the-struct . body)
                (with-syntax ([short-field-bindings
                                with-stx (map (λ (x) (list
                                                      (list (string->symbol (format "~a-~a" (syntax->datum #'struct-name) x))
                                                            (syntax->datum #'the-struct))))
                                              (syntax->datum #'field-names)))])
                  #'(let short-field-bindings
                      . body))]))))]))

(struct/with str (field))
(define x (str 1))
(with-str x (displayln field))

This paste has no annotations.

Colorize as:
Show Line Numbers

Lisppaste pastes can be made by anyone at any time. Imagine a fearsomely comprehensive disclaimer of liability. Now fear, comprehensively.