lists-js
v1.0.3
Published
list data structure based on pairs
Downloads
5
Readme
js-lists
Install
npm install lists-js
Using
import {cons, head, tail, isEmpty, toString, has, count, reverse, isList, push,
concat, map, filter, reduce, } from 'lists-js'
const list = cons(1, 2, 3); // (1, 2, 3)
head(list); // 1
tail(list); // (2, 3)
isList(list); // true
isList('text'); // false
push(0, list); // (0, 1, 2, 3)
toString(list); // (1, 2, 3)
has(3, list); // true
count(list); // 3
Documentation
Table of Contents
cons
Make a list
Parameters
args
...any
Examples
cons(1, 2, 3); // (1, 2, 3)
cons(); // ()
Returns list
isList
Check if arguments is list
Parameters
list
Examples
isList(cons()); // true
isList(cons(1, 2)); // true
isList('text'); // false
Returns boolean
head
Get head element
Parameters
list
Examples
head(cons(1, 2)); // 1
Returns any
tail
Get tail element
Parameters
list
Examples
tail(cons(1, 2, 3)); // (2, 3)
Returns any
isEmpty
Check if list is empty
Parameters
list
Examples
isEmpty(cons(1, 2)); // false
isEmpty(cons()); //true
Returns boolean
push
Add element to list
Parameters
element
list
Examples
push(3, cons(2, 1)); // (3, 2, 1)
Returns list
toString
Convert list to string
Parameters
list
Examples
toString(cons(1, 2, 3)); // (1, 2, 3);
toString(cons(1, cons(2, 3))); // (1, (2, 3));
Returns string
has
Check if list has an element
Parameters
value
list
Examples
has(1, cons(1, 2)); // true
has(5, cons(1, 2)); // false
Returns boolean
count
Get number of elements in a list
Parameters
list
Examples
count(cons(1, 2, 3)); // 3
count(cons()); // 0
Returns int
reverse
Reverse list
Parameters
list
Examples
reverse(cons(1, 2, 3)); // (3, 2, 1)
Returns list
concat
Join 2 lists
Parameters
list1
list2
Examples
concat(cons(1, 2), cons(3, 4)); // (1, 2, 3, 4)
Returns list
map
Map list
Parameters
f
list
Examples
const list = cons(1, 2, 3);
map(el => el ** 2, list); // (1, 4, 9)
Returns list
filter
Filter list
Parameters
f
list
Examples
const list = cons(-1, 2, -5, 0, 9);
filter(el => el > 0, list); // (2, 9)
Returns list
reduce
Reduce list
Parameters
f
acc
elements
Examples
reduce((el, acc) => acc + 1, 0, cons(1, 2, 3)); // 3
Returns any