Wolfram Computation Meets Knowledge

CacheTo

Contributed by: Taliesin Beynon

Returns a stored expression if it exists; otherwise, evaluates and stores an expression

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

returns symbol[key] if it exists, otherwise 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]=

Resource History