3.2.5. effect.ref module

class effect.ref.Reference(initial)

Bases: object

An effectful mutable variable, suitable for sharing between multiple logical threads of execution, that can be read and modified in a purely functional way.

Compare to Haskell’s IORef or Clojure’s atom.

Note:Warning: Instantiating a Reference causes an implicit side-effect. In other words, Reference is not a referentially transparent function, and you can’t use equational reasoning on it: a call to Reference is not interchangeable with the result of a call to Reference, since identity matters. If you want to create references in purely functional code, you can use the effect.Func intent: effect.Effect(effect.Func(Reference, initial)).
modify(transformer)

Return an Effect that updates the value with fn(old_value).

Parameters:transformer – Function that takes old value and returns the new value.

This is not guaranteed to be linearizable if multiple threads are modifying the reference at the same time. It is safe to assume consistent modification as long as you’re not using multiple threads, though.

read()

Return an Effect that results in the current value.

class effect.ref.ReadReference(ref)

Bases: object

Intent that gets a Reference’s current value.

class effect.ref.ModifyReference(ref, transformer)

Bases: object

Intent that modifies a Reference value in-place with a transformer func.

This intent is not necessarily linearizable if multiple threads are modifying the same reference at the same time.

effect.ref.perform_read_reference(*args, **kwargs)

Performer for ReadReference.

effect.ref.perform_modify_reference(*args, **kwargs)

Performer for ModifyReference.

This performer is not linearizable if multiple physical threads are modifying the same reference at the same time.