From 00f82f1f9379eb86480ded59da31a37b83b779cc Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Thu, 20 Nov 2014 10:16:06 -0800 Subject: [PATCH] add sugar/cache module --- cache.rkt | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 cache.rkt diff --git a/cache.rkt b/cache.rkt new file mode 100644 index 0000000..c44f26b --- /dev/null +++ b/cache.rkt @@ -0,0 +1,15 @@ +#lang racket/base +(require (for-syntax racket/base)) +(provide (all-defined-out)) + +(define (make-caching-proc base-proc) + (let ([cache (make-hash)]) + (λ args + (hash-ref! cache args (λ () (apply base-proc args)))))) + +(define-syntax (define/caching stx) + (syntax-case stx () + [(_ (name arg ... . rest-arg) body ...) + #'(define/caching name (λ(arg ... . rest-arg) body ...))] + [(_ name body ...) + #'(define name (make-caching-proc body ...))])) \ No newline at end of file