From 4632efc71dc658d8e5766c88658cf9631881e7cf Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 14 Feb 2019 12:44:48 -0500 Subject: [PATCH] Added Cyc-memoize --- srfi/69.sld | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/srfi/69.sld b/srfi/69.sld index f63daeff..4e6fa39a 100644 --- a/srfi/69.sld +++ b/srfi/69.sld @@ -37,6 +37,8 @@ string-hash string-ci-hash hash-by-identity + ;; Cyclone Custom + Cyc-memoize ) (import (scheme base) (scheme char) @@ -309,4 +311,25 @@ (define (hash-table-values hash-table) (hash-table-fold hash-table (lambda (key val acc) (cons val acc)) '())) +;; Cyclone-specific +;; +;; Take a function and return another function that will store the results +;; of calling the original function, and return those cached results on +;; subsequent requests. +(define (Cyc-memoize function) + (let ((table (make-hash-table))) ;(make-equal?-map))) + (lambda args + (apply values + ;(map-get table + (hash-table-ref table + args + ;; If the entry isn't there, call the function. + (lambda () + (call-with-values + (lambda () (apply function args)) + (lambda results + ;(map-put! table args results) + (hash-table-set! table args results) + results)))))))) + ))