]> gitweb.factorcode.org Git - factor.git/commitdiff
memoize.syntax: adding IDENTITY-MEMO[
authorJohn Benediktsson <mrjbq7@gmail.com>
Thu, 26 Oct 2023 17:09:46 +0000 (10:09 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Thu, 26 Oct 2023 17:09:46 +0000 (10:09 -0700)
basis/memoize/syntax/syntax-docs.factor
basis/memoize/syntax/syntax.factor
core/memoize/memoize.factor

index d4eba04bb05e153442409177f9d33a4fde330556..923b2a4c560b3d79a055f4ece0b30590e889273c 100644 (file)
@@ -4,3 +4,7 @@ USING: help.markup help.syntax memoize.syntax ;
 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." } ;
index d95a2f79c7bdcf0631b5df81bd4a1b54e7245071..fbf97a9e3e6d0b345391b469672183ca3890ec75 100644 (file)
@@ -2,4 +2,8 @@ USING: kernel memoize parser sequences stack-checker ;
 
 IN: memoize.syntax
 
-SYNTAX: MEMO[ parse-quotation dup infer memoize-quot suffix! ;
+SYNTAX: MEMO[
+    parse-quotation dup infer memoize-quot suffix! ;
+
+SYNTAX: IDENTITY-MEMO[
+    parse-quotation dup infer identity-memoize-quot suffix! ;
index a0a3d3a5bd887bd7870a64fd7787c4fca4f605dc..9a7a607ddea88d95f579114a1265813bbc4afe0a 100644 (file)
@@ -55,6 +55,12 @@ IN: memoize
 : 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 -- )
@@ -64,12 +70,10 @@ PRIVATE>
     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 ;
 
@@ -85,8 +89,10 @@ M: memoized reset-word
     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?