Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cronokirby/persistent-ts
Persistent data structures for Typescript
https://github.com/cronokirby/persistent-ts
algorithms data-structures functional immutable javascript typescript
Last synced: 26 days ago
JSON representation
Persistent data structures for Typescript
- Host: GitHub
- URL: https://github.com/cronokirby/persistent-ts
- Owner: cronokirby
- License: mit
- Created: 2019-06-09T21:37:02.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-21T07:28:32.000Z (about 1 year ago)
- Last Synced: 2024-04-27T07:34:17.544Z (8 months ago)
- Topics: algorithms, data-structures, functional, immutable, javascript, typescript
- Language: TypeScript
- Size: 521 KB
- Stars: 8
- Watchers: 1
- Forks: 2
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# persistent-ts
Persistent data structures for TypeScript.
Persistent data structures are entirely immutable, but efficiently share elements between
each other. For example, prepending to a persisistent list only creates a new node that
references the old list. Prepending to an array, however, requires copying an entire array.
Persistent data structures are thus better suited for use in immutable environments.This library isn't as developed as others, such as
[immutable js](https://github.com/immutable-js/immutable-js).
However, it can provide alternative data structures, and hopefully more readable implementations.
Unlike _immutable js_ specifically, this implementation is also Typescript first, whereas
that library adds type annotations after the fact. This makes for an api centered around
generics, and not necessarily acccomadating JS' indiosyncracies.## Data Structures Implemented
This library only implements a handful of data structures at the moment.
### List
`List` is a singly linked list. Here's a sample of its operations:
```ts
// ()
List.empty();// (1, 2, 3, 4)
List.of(1, 2, 3, 4);// (1, 2)
List.of(1).prepend(2);// 1
List.of(1, 2, 3).head();// (2, 3)
List.of(1, 2, 3).tail();// (1, 2, 3)
List.of(1, 2, 3, 4).take(3);// (4)
List.of(1, 2, 3, 4).drop(3);//[1, 2, 3, 4]
[...List.of(1, 2, 3, 4)];
```### Vector
`Vector` is a Radix Tree in the vein of clojure's data structure.
This can be used as an immutable sequence with efficient random access and
appending. Here's a sample of its operations:```ts
// []
Vector.empty();// [1]
Vector.empty().append(1);// []
Vector.of(1).pop();// 3
Vector.of(1, 2, 3).get(2);// [1, 2, 100]
Vector.of(1, 2, 3).set(2, 100);
```