If every time you want to change something in a data structure, you just go and change it, your code will be full of side-effects. On the other hand, copying complete structures every time is a waste of time and space. There's a middle way, with persistent data structures, which, if handled correctly, let you apply changes while creating new structures, in an efficient way.
Persistent data structures
Working with lists
Let's consider a simple procedure: suppose you have a list, and you want to add a new element to it. How would you do it? We can assume each node is a NodeList object.
class ListNode {
constructor(value, next = null) {
this.value = value;
this.next = next;
}
}
A possible list would be...