Standard Library: Ref
Work with mutable state
The core data structures in Pointless are immutable. References serve as mutable wrappers around these values, allowing programs to leverage mutability. Note that references must not reference themselves (they must not contain circular references) as this could cause memory leaks and infinite recursion.
An example pseudorandom number generator function that uses a reference to store the generator state.
seed = Ref.of(0)
-- Linear congruential generator parameters
m = 2 ** 32
a = 1664525
c = 1013904223
fn random()
next = (a * Ref.get(seed) + c) % m -- Calculate new seed
Ref.set(seed, next) -- Store new seed
next / m -- Return scaled value
end
random()
random()
random()
random()
0.23606797284446657
0.278566908556968
0.8195337599609047
0.6678668977692723
Ref.get(ref)
Get the value stored in ref.
score = Ref.of(0)
Ref.get(score)
0
Ref.of(value)
Create a mutable reference containing value.
Ref.of(0)
Ref.of(0)
Ref.put(value, ref)
Update ref to store value. Return
value.
score = Ref.of(0)
Ref.put(1, score)
score
1
Ref.of(1)
Ref.set(ref, value)
Update ref to store value. Return
ref.
score = Ref.of(0)
Ref.set(score, 1)
score
Ref.of(1)
Ref.of(1)