@collectable/list
v5.1.0
Published
[Collectable.js] Immutable List
Downloads
89
Readme
Immutable List
A persistent/immutable/functional vector structure based on a modified RRB Tree
This package provides a Clojure-style persistent vector based on a modified RRB Tree implementation, with very fast concatenation, insertion and deletion of ranges of values, etc.
This documentation is under construction. The list of functions below is comprehensive, but descriptions and examples are pending.
Installation
# via NPM
npm install @collectable/list
# or Yarn
yarn add @collectable/list
If you intend to use other data structures as well, install the main collectable package instead. It takes a dependency on each of these data structures, and so they will become available implicitly, after installation.
# via NPM
npm install collectable
# or Yarn
yarn add collectable
TypeScript type definitions are built in.
Usage
Import and use the functions you need:
import { fromArray, arrayFrom } from '@collectable/list';
const list = fromArray(['X', 'Y']);
const array = arrayFrom(list);
Use a modern bundler such as Webpack 2 or Rollup in order to take advantage of tree shaking capabilities, giving you maximum flexibility to use what you need while excluding anything else from the final build.
API
All list-manipulation functions are available from module @collectable/list
.
Creating a list
empty<T>(): List<T>
fromArray<T>(values: T[]): List<T>
fromIterable<T>(values: Iterable<T>): List<T>
fromArgs<T>(...values: T[]): List<T>
Appending and prepending values
append<T>(value: T, list: List<T>): List<T>
Appends a new value to the end of a list, growing the size of the list by one.
- value: The value to append to the list
- list: The list to which the value should be appended
- returns: A list containing the appended value
// Primary API
import { fromArray, append } from '@collectable/list';
const two = fromArray(['X', 'Y']); // => [X, Y]
const three = append('Z', two); // => [X, Y, Z]
// Curried API
import { fromArray, append } from '@collectable/list/curried';
const two = fromArray(['X', 'Y']); // => [X, Y]
const addZ = append('Z');
const three = addZ(two); // => [X, Y, Z]
appendArray<T>(values: T[], list: List<T>): List<T>
Appends an array of values to the end of a list, growing the size of the list by the number of elements in the array.
- value: The values to append to the list
- list: The list to which the values should be appended
- returns: A list containing the appended values
appendIterable<T>(values: Iterable<T>, list: List<T>): List<T>
Appends a set of values to the end of a list, growing the size of the list by the number of elements iterated over.
- value: The values to append to the list
- list: The list to which the values should be appended
- returns: A list containing the appended values
prepend<T>(value: T, list: List<T>): List<T>
prependArray<T>(values: T[], list: List<T>): List<T>
prependIterable<T>(values: Iterable<T>, list: List<T>): List<T>
Updating existing values
set<T>(index: number, value: T, list: List<T>): List<T>
updateList<T>(callback: UpdateListCallback<List<T>>, list: List<T>): List<T>
type UpdateListCallback<T> = (value: T) => T|void
update<T>(index: number, callback: UpdateIndexCallback<T|undefined>, list: List<T>): List<T>
type UpdateIndexCallback<T> = (value: T) => T