liveBook · Manning

Write operation

As explained in Grokking Simplicity

A write operation modifies the stored value or state of data.

A write operation modifies data. Adding an item, removing an item, updating a quantity, or setting a price are writes; reads retrieve information without changing it. section 6.2

In JavaScript, mutable data is the default, so writes require discipline when other code may still use the original value. Copy-on-write preserves immutability by making a copy, modifying that copy, and returning it. section 6.2 section 6.3

Apply the discipline in three steps:

Separate a combined operation

When an operation both reads and writes, split it first. Read the first element with first_element(array), then remove it with a copy-on-write drop_first(array). This makes each piece independently reusable and testable. section 6.9

function add_element_last(array, elem) { #1
    var new_array = array.slice(); #2
    new_array.push(elem); #3
    return new_array; #4
}

Trace a compact JavaScript implementation that copies an array, modifies the copy, and returns it. section 6.3

How Grokking Simplicity covers it

How deep it goes
Defines writes, gives a three-step copy-on-write procedure, and separates combined reads from writes.
Examples
Uses shopping-cart updates, item pricing, array insertion, and array removal to illustrate writes.
What it assumes
Assumes basic JavaScript arrays and distinguishes mutable from immutable data through concrete operations.
What you can do after
Build it from scratch using copy-on-write and split combined reads from writes for independent reuse.
How it connects
Connects writes with reads, immutable data structures, calculations, and copy-on-write.

Also explained in

Compare how 2 books explain write operation →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Simplicity. The section links point to the parts of the book it draws on.