"Value" (Data Structure)
"Value"
represents a mutable expression value.
Details
- A mutable value can be modified in any part of a program:
-
CreateDataStructure["Value",x] create a new "Value" with specified initial value x Typed[x,"Value"] give x the type "Value" - For a data structure of type "Value", the following operations can be used:
-
ds["Copy"] return a copy of ds time: O(1) ds["Get"] get the value stored in ds time: O(1) ds["Set",x] set the value of ds to x time: O(1) ds["Visualization"] return a visualization of ds time: O(1) - The following functions are also supported:
-
dsi===dsj True, if dsi equals dsj FullForm[ds] the full form of ds Information[ds] information about ds InputForm[ds] the input form of ds Normal[ds] convert ds to a normal expression
Examples
open all close allBasic Examples (1)
A new "Value" can be created with CreateDataStructure:
ds = CreateDataStructure["Value", f[1]]ds["Get"]Insert a new value to be stored:
ds["Set", f[2]]Confirm that the value has updated:
ds["Get"]Return an expression version of ds:
Normal[ds]A visualization of the data structure can be generated:
ds["Visualization"]Scope (6)
Mutability (1)
The "Value" data structure is mutable, so its value can be changed:
ds = CreateDataStructure["Value", f[x]]ds["Set", g[y]]ds["Get"]ds["Set", h[z]]ds["Get"]Information (1)
A new "Value" can be created with CreateDataStructure:
ds = CreateDataStructure["Value", f[1]]Information about the data structure ds:
Information[ds]Operations (4)
"Copy" (1)
Create a new "Value" with an initial value:
ds = CreateDataStructure["Value", f[x]]Get a copy of the data structure:
ds1 = ds["Copy"]The contents of both data structures are the same:
{Normal[ds], Normal[ds1]}Modifying the copy will not affect the original data structure:
ds1["Set", g[x]];
{Normal[ds], Normal[ds1]}"Get" (1)
Create a new "Value" with an initial value:
ds = CreateDataStructure["Value", f[x]]ds["Get"]"Set" (1)
Create a new "Value" with an initial value:
ds = CreateDataStructure["Value", f[x]]Set the data structure to a new expression:
ds["Set", g[x]]ds["Get"]"Visualization" (1)
Create a new "Value" with an initial value:
ds = CreateDataStructure["Value", f[x]]ds["Visualization"]Applications (1)
In-Place Update of an Expression (1)
Define a function that updates a "Value" passed as an argument:
accumulate[f_, val_] /; DataStructureQ[val, "Value"] := val["Set", f[val["Get"]]];Create a "Value" holding an initial expression:
v = CreateDataStructure["Value", x]Update the stored expression through repeated function calls:
accumulate[# ^ 2&, v];
accumulate[# + 1&, v];v["Get"]Properties & Relations (4)
InputForm (1)
Normal (1)
SameQ (1)
SameQ can be used to test whether two "Value" data structures have the same contents:
ds1 = CreateDataStructure["Value", f[x]];
ds2 = CreateDataStructure["Value", f[x]];
ds1 === ds2If one element is changed, the data structures are no longer the same:
ds1["Set", g[x]];
ds1 === ds2Neat Examples (1)
Memoized Evaluation with Explicit Cache (1)
Use a "Value" to implement a manually controlled cache that can be reset or copied:
cache = CreateDataStructure["Value", <||>]Define a function that consults and updates the cache:
cachedEval[f_, x_] :=
With[{c = cache["Get"]},
If[KeyExistsQ[c, x],
c[x]
,
cache["Set", Append[c, x -> f[x]]][x]
]
]Evaluate the function, populating the cache:
cachedEval[# ^ 2&, 10];
cachedEval[# ^ 2&, 11];cache["Get"]Related Guides
History
Introduced in 2020 (12.1)