Transients

Although Transient Data Structures are not reference types, a transient can be used as an efficient alternative to a Var. Unlike most of Clojure's data types, transients are mutable:

Rationale

If a tree falls in the woods, does it make a sound?

If a pure function mutates some local data in order
to produce an immutable return value, is that ok?

Mutability is often considered a Bad Thing by functional programming advocates. However, the Real Problem (IMHO) is the proper management of shared mutable state. The Clojure folks appear to agree with this perspective, deciding that mutable state is safe, as long as it isn't shared. See Transient Data Structures for the full rationale and details.

Overview

Like Vars, transients are isolated, synchronous, and autonomous:

  • isolated - each thread has access to its own item

  • synchronous - requested changes occur immediately

  • autonomous - changes are made to a single item

Example: Add 1 to this thread's version of bar, right now.

A transient is a mutable, local copy (via structural sharing) of a persistent, immutable data structure. It has full access to the base structure, but cannot affect it. And, before the copy can be shared (e.g., returned from a function), it must be converted back into immutable form.

Because transients aren't reference types, they can be manipulated directly. This makes the access code cleaner, simpler, and much faster. In summary, transients provide a convenient and efficient way to deal with mutable storage locations.

To be continued...


This wiki page is maintained by Rich Morin, an independent consultant specializing in software design, development, and documentation. Please feel free to email comments, inquiries, suggestions, etc!

Topic revision: r2 - 27 Aug 2015, RichMorin
This site is powered by Foswiki Copyright © by the contributing authors. All material on this wiki is the property of the contributing authors.
Foswiki version v2.1.6, Release Foswiki-2.1.6, Plugin API version 2.4
Ideas, requests, problems regarding CFCL Wiki? Send us email