Functional library for manipulation of pairs-based data structures with Sass-like API. Original ideas from SICP.
Library for study. Not for production projects.
The library contains 3 data structures and functions for working with them. Key features:
- creation and manipulation occur using functions
- objects are not used
- structures are immutable
Functions for creating are called constructors. Functions for getting data are called selectors.
The library consists of the following structures:
- pair
- list
- map
All structures based on pairs.
Pair is simple structure that consist of 2 any type elements. Pair may contain other pairs.
import { pair, fir, sec, toString } from 'pbsl/pair';
const myPair = pair(1, 2);
const nestedPair = pair('testou', myPair);
fir(myPair) // 1
sec(myPair) // 2
toString(myPair) // (1, 2)
toString(nestedPair) // (testou, (1, 2))
The list is an orderly sequence of any elements. By default, the list are typed by first item. Also you can specify the type manually.
import { pair } from 'pbsl/pair';
import { l, top, tail, toString, prepand } from 'pbsl/list';
const myList = l(2, 5, 6);
const pairInList = l<unknown>(pair(1, 0), 'item', 'end');
top(myList) // 2
tail(myList) // l(5, 6)
prepand(myList, 0) // (0, 2, 5, 6)
toString(myList) // (2, 5, 6)
toString(pairInList) // ((1, 0), item, end)
Under the hood, list is a nested pairs where second element of last pair is a null
. Empty list also is null
.
l(2, 5, 6) // pair(2, pair(5, pair(6, null)))
For working with the lists are available some functions with Sass-like API:
append
index
join
length
nth
setNth
And main functions for working with sequences with js Array-like API:
map
filter
reduce
find
findIndex
Map is a list
of pairs
in the 'key-value' format. The map is created based on array with a tuples with 2 elements: key and value. Typed like a list with ability to specify a types.
import { mkMap, get, set } from 'pbsl/map';
import { toString } from 'pbsl/list';
const myMap = mkMap([['name', 'mister'], ['age', '54']]);
const codes = mkMap([[200, 'ok'], [403, 'forbidden'], [404, 'not found']]);
toString(myMap) // ((name, mister), (age, 54))
get(myMap, 'name') // 'mister'
toString(set(myMap, 'race', 'human')) // ((race, human), (name, mister), (age, 54))
For working with maps also used Sass-like API. The following functions are available:
get
set
has
remove
merge
keys
values
npm i -D pbsl
MIT