https://github.com/noughtmare/fleet-array
A modern implementation of diff arrays
https://github.com/noughtmare/fleet-array
array data-structures persistent-data-structure
Last synced: 5 months ago
JSON representation
A modern implementation of diff arrays
- Host: GitHub
- URL: https://github.com/noughtmare/fleet-array
- Owner: noughtmare
- License: bsd-3-clause
- Created: 2025-01-24T21:01:30.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-03-12T19:27:12.000Z (9 months ago)
- Last Synced: 2025-04-22T10:41:50.431Z (8 months ago)
- Topics: array, data-structures, persistent-data-structure
- Language: Haskell
- Homepage:
- Size: 61.5 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fleet Array
Situation:
* There is one essential idea that I must explain properly. Usually in Haskell
you think of everything as a value. However, under the hood most things are
implemented using references. Everytime I say "old version" or "old
reference", I refer to this implementation detail. There is no way to really
explain it without explaining how references are used in the implementation of
Haskell.
* Using immutable data structures makes it easier to reason about your programs
(motivation)
* All references to an immutable data structure have the same value
(restriction)
* Mutation can always be avoided by creating a modified copy instead (bad
previous solution).
* Copying can be cheap, if the data that needs to be copied is not much larger
than the data that has been changed
* That happens, for example, when changing the first element of a linked list.
The remainder of the list can be shared in memory.
Complication (why is it bad?):
* Arrays, on the other hand, are an example of a data structure where changes
can be much smaller than the copied data.
* In an array, changing a single element would necessitate a copy of the entire
array.
* Thus, such copying unfortunately has a high performance cost.
* Copying can turn constant-time operations into linear-time operations (and
linear-time into quadratic-time, etc.).
Question:
* How can we avoid mutation without resorting to expensive copying?
Answer:
* Do the mutation, but keep track of changes
* Old references to the same data will be mutated in an equal but opposite way
* Whenever the old version is used after the mutation, reapply the changes