HELP: MEMO[
{ $syntax "MEMO[ elements... ]" }
{ $description "Defines the given literal quotation as one which memoizes its outputs given a particular input." } ;
+
+HELP: IDENTITY-MEMO[
+{ $syntax "IDENTITY-MEMO[ elements... ]" }
+{ $description "Defines the given literal quotation as one which memoizes its outputs given a particular input which is identical to another input." } ;
: make-memoizer ( table quot effect -- quot )
dup in>> length zero? [ make/0 ] [ make/n ] if ;
+: memo-cache ( effect -- cache )
+ in>> length zero? [ f f 2array ] [ H{ } clone ] if ;
+
+: identity-memo-cache ( effect -- cache )
+ in>> length zero? [ f f 2array ] [ IH{ } clone ] if ;
+
PRIVATE>
: (define-memoized) ( word quot effect hashtable -- )
3tri ;
: define-memoized ( word quot effect -- )
- dup in>> length zero? [ f f 2array ] [ H{ } clone ] if
- (define-memoized) ;
+ dup memo-cache (define-memoized) ;
: define-identity-memoized ( word quot effect -- )
- dup in>> length zero? [ f f 2array ] [ IH{ } clone ] if
- (define-memoized) ;
+ dup identity-memo-cache (define-memoized) ;
PREDICATE: memoized < word "memoize" word-prop >boolean ;
bi ;
: memoize-quot ( quot effect -- memo-quot )
- dup in>> length zero? [ f f 2array ] [ H{ } clone ] if
- -rot make-memoizer ;
+ dup memo-cache -rot make-memoizer ;
+
+: identity-memoize-quot ( quot effect -- memo-quot )
+ dup identity-memo-cache -rot make-memoizer ;
: reset-memoized ( word -- )
"memoize" word-prop dup sequence?