Skip to content

Instantly share code, notes, and snippets.

@dyoo
Created August 7, 2012 20:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save dyoo/3289283 to your computer and use it in GitHub Desktop.
proc/data
#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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment