]> gitweb.factorcode.org Git - factor.git/commitdiff
memoize: faster memoize for single argument words.
authorJohn Benediktsson <mrjbq7@gmail.com>
Fri, 24 Aug 2012 00:39:07 +0000 (17:39 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Fri, 24 Aug 2012 00:39:07 +0000 (17:39 -0700)
basis/memoize/memoize.factor

index a1d4da62dc485d43289ddd3adfb28e46c8454d0e..7f62a8c1617685086d63ffba6388a6e3dee41237 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2007, 2010 Slava Pestov, Daniel Ehrenberg.
 ! See http://factorcode.org/license.txt for BSD license.
-USING: kernel hashtables sequences sequences.private arrays
-words namespaces make parser effects.parser math assocs effects
-definitions quotations summary accessors fry hashtables.identity ;
+USING: accessors arrays assocs combinators definitions effects
+effects.parser fry hashtables.identity kernel math sequences
+sequences.private words ;
 IN: memoize
 
 <PRIVATE
@@ -41,6 +41,10 @@ IN: memoize
 : make/n ( table quot effect -- quot )
     [ unpack/pack '[ _ _ cache ] ] keep pack/unpack ;
 
+: make/1 ( table quot effect -- quot )
+    out>> [ packer compose '[ _ _ cache ] ] keep
+    unpacker compose ;
+
 : make/0 ( table quot effect -- quot )
     out>> [
         packer '[
@@ -50,7 +54,11 @@ IN: memoize
     ] keep unpacker compose ;
 
 : make-memoizer ( table quot effect -- quot )
-    dup in>> length zero? [ make/0 ] [ make/n ] if ;
+    dup in>> length {
+        { 0 [ make/0 ] }
+        { 1 [ make/1 ] }
+        [ drop make/n ]
+    } case ;
 
 PRIVATE>