Wolfram Research

Function Repository Resource:

CacheTo

Source Notebook

Return a stored expression if it exists; otherwise, evaluate and store an expression

Contributed by: Taliesin Beynon

ResourceFunction["CacheTo"][symbol, key, expr]

returns symbol[key] if it exists; otherwise, it evaluates expr, stores it under symbol[key] and returns expr.

Details and Options

ResourceFunction["CacheTo"] has the attribute HoldAll.

Examples

Basic Examples

Generate a cache for an expression:

In[1]:=
ResourceFunction["CacheTo"][k, g, {{a, b}, {c, d}}]
Out[1]=

Recover the cached value:

In[2]:=
k
Out[2]=
In[3]:=
k[g]
Out[3]=

No value for key h:

In[4]:=
k[h]
Out[4]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Resource History

License Information