liveBook · Manning

Immutable function

As explained in Data-Oriented Programming: Reduce software complexity

An immutable function is a function that manipulates data without mutating existing data, producing new data instead.

An immutable function changes data without changing the value it receives. Instead, it returns a new version, leaving the original available for comparison, debugging, or restoration. section 4.8

Suppose a library system must change Watchmen’s publication year. An immutable update follows the information path to that field, returns updated library data, and preserves the previous version. Structural sharing keeps unchanged parts common between versions rather than copying everything. section 4.2

A safe immutable-update design follows these rules:

  • Use immutable functions for every data manipulation; do not use the native hash map setter. section 4.4
  • Separate calculation from commit: calculate a new state version without side effects, then move the system reference forward. section 4.8
  • Expect shared data to remain safe only when all updates use immutable operations. section 4.4

Updating a nested field

var nextLibraryData = _.set(libraryData, ["catalog", "booksByIsbn", "978-1779501127", "publicationYear"], 1986);

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Defines returned data versions, structural sharing, calculation and commit phases, and mutation hazards; defers implementation beyond Lodash usage.
Examples
Uses a Watchmen catalog update, nested Lodash _.set calls, and a shared-array mutation that changes both observed versions.
What you can do after
Apply it via Lodash FP to create efficient new state versions while preserving prior data.
How it connects
Connects immutable functions with structural sharing, persistent data structures, state management, and calculation and commit phases.

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.