memoize: faster memoize for single argument words.

db4
John Benediktsson 2012-08-23 17:39:07 -07:00
parent daed497e3d
commit 990fdca1fc
1 changed files with 12 additions and 4 deletions

View 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>