liveBook · Manning

Immutable collection

As explained in Data-Oriented Programming: Reduce software complexity

Immutable data is data whose stored value cannot be modified after creation, requiring changes to be represented by new data versions.

An immutable collection preserves its stored value after creation. Operations that appear to modify it instead produce a new collection version, preventing accidental mutation and making behavior easier to predict. section 9.1 section 1.2.2

Use immutable collections to:

  • Share data safely between components and pass it through generic data-manipulation functions. section 11.0
  • Represent state as multiple preserved versions rather than updating one collection in place. section 4.0
  • Support deterministic replay when function contexts contain serializable immutable data. section 15.7

Creating a changed version

Immutable.js uses accessors such as Immutable.getIn() and creates changed versions with Immutable.setIn(). A publication year can therefore change without modifying the original nested collection. section 9.3.2

Listing 4.2 Using _.set as an immutable function
var nextLibraryData = _.set(libraryData, 
  ["catalog", "booksByIsbn",
    "978-1779501127", "publicationYear"],
  1986);

Inspect the Lodash FP example that updates a nested library value immutably through an information path. section 4.2

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Explains freezing, persistent data structures, structural sharing, versioned state, and library APIs; defers detailed concurrency control to later treatment.
Examples
Demonstrates Java immutable lists and maps, JavaScript freezing, Lodash FP updates, Immutable.js maps, and library-state versions.
What it assumes
Assumes basic collections and mutation; teaches the distinction between immutable collections and persistent data structures through concrete examples.
What you can do after
Apply it via Java, JavaScript, Lodash FP, or Immutable.js to preserve collections and create changed versions.
How it connects
Connects immutable collections with data immutability, persistent data structures, structural sharing, state management, concurrency control, and serialization.

Also explained in

Compare how 24 books explain immutable collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.