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