Folds over the error and value types of the ZRef
.
Folds over the error and value types of the ZRef
. This is a highly
polymorphic method that is capable of arbitrarily transforming the error
and value types of the ZRef
. For most use cases one of the more specific
combinators implemented in terms of fold
will be more ergonomic but this
method is extremely useful for implementing new combinators.
Folds over the error and value types of the ZRef
, allowing access to the
state in transforming the set
value.
Folds over the error and value types of the ZRef
, allowing access to the
state in transforming the set
value. This is a more powerful version of
fold
but requires unifying the error types.
Reads the value from the ZRef
.
Writes a new value to the ZRef
, with a guarantee of immediate consistency
(at some cost to performance).
Writes a new value to the ZRef
without providing a guarantee of immediate
consistency.
Maps and filters the get
value of the ZRef
with the specified partial
function, returning a ZRef
with a get
value that succeeds with the
result of the partial function if it is defined or else fails with None
.
Transforms the set
value of the ZRef
with the specified function.
Transforms the set
value of the ZRef
with the specified fallible
function.
Transforms both the set
and get
values of the ZRef
with the specified
functions.
Transforms both the set
and get
values of the ZRef
with the specified
fallible functions.
Transforms both the set
and get
errors of the ZRef
with the specified
functions.
Filters the set
value of the ZRef
with the specified predicate,
returning a ZRef
with a set
value that succeeds if the predicate is
satisfied or else fails with None
.
Filters the get
value of the ZRef
with the specified predicate,
returning a ZRef
with a get
value that succeeds if the predicate is
satisfied or else fails with None
.
Transforms the get
value of the ZRef
with the specified function.
Transforms the get
value of the ZRef
with the specified fallible
function.
Returns a read only view of the ZRef
.
Returns a write only view of the ZRef
.
A
ZRef[RA, RB, EA, EB, A, B]
is a polymorphic, purely functional description of a mutable reference. The fundamental operations of aZRef
areset
andget
.set
takes a value of typeA
and sets the reference to a new value, requiring an environment of typeRA
and potentially failing with an error of typeEA
.get
gets the current value of the reference and returns a value of typeB
, requiring an environment of typeRB
and potentially failing with an error of typeEB
.When the error and value types of the
ZRef
are unified, that is, it is aZRef[R, R, E, E, A, A]
, theZRef
also supports atomicmodify
andupdate
operations. All operations are guaranteed to be safe for concurrent access.By default,
ZRef
is implemented in terms of compare and swap operations for maximum performance and does not support performing effects within update operations. If you need to perform effects within update operations you can create aZRef.Synchronized
, a specialized type ofZRef
that supports performing effects within update operations at some cost to performance. In this case writes will semantically block other writers, while multiple readers can read simultaneously.ZRef.Synchronized
also supports composing multipleZRef.Synchronized
values together to form a singleZRef.Synchronized
value that can be atomically updated using thezip
operator. In this case reads and writes will semantically block other readers and writers.NOTE: While
ZRef
provides the functional equivalent of a mutable reference, the value inside theZRef
should normally be immutable since compare and swap operations are not safe for mutable values that do not support concurrent access. If you do need to use a mutable valueZRef.Synchronized
will guarantee that access to the value is properly synchronized.