PasteRack.org
Paste # 7761
2023-11-01 17:17:18

Fork as a new paste.

Paste viewed 485 times.


Embed:

  1. #lang racket
  2.  
  3. (require (for-syntax racket/syntax
  4.                      syntax/parse))
  5.  
  6. (define-syntax (define-cached stx)
  7.   (syntax-parse stx
  8.     [(_ (fun-id args ...) body ...)
  9.      #:with fresh? (format-id stx "fresh?")
  10.      #'(define (fun-id args ... #:fresh? [fresh? #f])
  11.          (define cached (fetch-funcall 'fun-id (list args ...)))
  12.          (cond [(and cached (not fresh?)) cached]
  13.                [else
  14.                 (define result (let () body ...))
  15.                 (cache-funcall 'fun-id (list args ...) result)]))]))
  16.  
  17. (define (cache-funcall name input result)
  18.   'cache-data)
  19.  
  20. (define (fetch-funcall name input)
  21.   'fetch-data)

=>