cache ( ... key assoc quot: ( ... key -- ... value ) -- ... value )
Factor handbook » The language » Collections » Associative mapping operations » Associative mapping combinators

Prev:assoc-reject! ( ... assoc quot: ( ... key value -- ... ? ) -- ... assoc )
Next:2cache ( ... key1 key2 assoc quot: ( ... key1 key2 -- ... value ) -- ... value )


Vocabulary
assocs

Inputs
keya key
assocan assoc
quota quotation with stack effect ( ... key -- ... value )


Outputs
valuea previously-retained or freshly-computed value


Word description
If the key is present in the assoc, outputs the associated value, otherwise calls the quotation to produce a value and stores the key/value pair into the assoc. Returns a value either looked up or newly stored in the assoc.

Side effects
Modifies assoc

Definition


: cache
( ... key assoc quot: ( ... key -- ... value ) -- ... value )
[ [ at* ] 2keep ] dip
[ [ nip call dup ] [ drop ] 3bi set-at ] 3curry
[ drop ] prepose unless ; inline