You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sugar/sugar/cache.rkt

25 lines
709 B
Racket

9 years ago
#lang racket/base
(require (for-syntax racket/base) "define.rkt")
9 years ago
9 years ago
(define+provide+safe (make-caching-proc base-proc)
(procedure? . -> . procedure?)
(let ([cache (make-hash)])
(make-keyword-procedure
(λ (kws kw-args . args)
9 years ago
(hash-ref! cache (list* kws kw-args args) (λ () (keyword-apply base-proc kws kw-args args)))))))
(define-for-syntax (lambdafy stx)
(syntax-case stx ()
[(_ (id arg ... . rest-arg) body ...)
#'(id (λ (arg ... . rest-arg) body ...))]
[(_ id body-exp)
#'(id body-exp)]))
9 years ago
(provide+safe define/caching)
(define-syntax (define/caching stx)
9 years ago
(with-syntax ([(id lambda-expr) (lambdafy stx)])
#'(define id (make-caching-proc lambda-expr))))