public
Created

proc/data

  • Download Gist
def.rkt
Racket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#lang racket
 
;; Let's create a custom structure for procedures that remember certain things.
(struct proc/data (proc data)
#:property prop:procedure (lambda (pd . args)
(apply (proc/data-proc pd) args)))
 
;; We can manually create such instances...
(define f (proc/data
;; implementation:
(lambda (x)
(* x x))
;; with associated metadata:
"this is the square function"))
 
;; It can be called:
(f 16)
 
;; but we can also retrieve its metadata:
(proc/data-data f)
 
 
;; Now let's have a def form that remembers module information.
 
 
;; This "def" will try to automatically bundle a metadata or a proc/data; this information is
;; the module path, which it can determine during compilation.
(define-syntax (def stx)
(syntax-case stx ()
[(_ (name . args) body ...)
(syntax/loc stx
(define name (proc/data
 
;; The procedure implementation as before:
(lambda args body ...)
;; Plus some metadata whose content we can compute at compile-time:
(let-syntax ([get-literal-metadata
(lambda (stx)
#`(#%datum . #,(format "~s" (variable-reference->resolved-module-path
(#%variable-reference)))))])
(get-literal-metadata)))))]))
 
(provide def
(struct-out proc/data))

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.