Standard
Import
To install, run either npm install --save @ayc0/std
or yarn add @ayc0/std
, and then:
const std = require('@ayc0/std');
const multiplyBy2 = std.map(x => x * 2);
const map = require('@ayc0/std/map');
const multiplyBy2 = map(x => x * 2);
const input = [1, 2];
const output = multiplyBy2(input);
Supported functions
For all of the functions listed below, you can either do std.<function>
or import them from @ayc0/std/<function>
:
Range
function* range([from,] to[, step]) {
}
Zip
function* zip(iterables) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
for (const [currentValues, indexes, iterables] of zip([
'123',
[1, 2, 3],
{ a: 1, b: 2, c: 3 },
])) {
console.log(currentValues);
console.log(indexes);
}
Len
function len(iterable) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Map
function map(callback[, thisArg]) {
return function (iterable) {
}
}
function callback(currentValue[, index[, iterable]]) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
map(x => x * 2)({ a: 1, b: 2 });
map(x => x * 2)([1, 2]);
map(x => x * 2)(new Set([1, 2]));
map(x => x * 2)(
new Map([
['a', 1],
['b', 2],
]),
);
map(x => x.repeat(2))('ab');
ForEach
function forEach(callback[, thisArg]) {
return function (iterable) {
}
}
function callback(currentValue[, index[, iterable]]) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
forEach(x => console.log(x * 2))({ a: 1, b: 2 });
forEach(x => console.log(x * 2))([1, 2]);
forEach(x => console.log(x * 2))(new Set([1, 2]));
forEach(x => console.log(x * 2))(
new Map([
['a', 1],
['b', 2],
]),
);
forEach(x => console.log(x.repeat(2)))('ab');
Filter
function filter(callback[, thisArg]) {
return function (iterable) {
}
}
function callback(currentValue[, index[, iterable]]) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
filter(x => x % 2)({ a: 1, b: 2 });
filter(x => x % 2)([1, 2]);
filter(x => x % 2)(new Set([1, 2]));
filter(x => x % 2)(
new Map([
['a', 1],
['b', 2],
]),
);
filter(x => x % 2)('12');
Reduce
function reduce(callback, initialValue, thisArg) {
return function (iterable) {
};
}
function callback(accumulator, currentValue[, index[, iterable]]) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
reduce((acc, x) => acc + x, 0)({ a: 1, b: 2 });
reduce((acc, x) => acc + x, 0)([1, 2]);
reduce((acc, x) => acc + x, 0)(new Set([1, 2]));
reduce(
(acc, x) => acc + x,
0,
)(
new Map([
['a', 1],
['b', 2],
]),
);
reduce((acc, x) => acc + Number(x), 0)('12');
Take
function take(limit) {
return function (iterable) {
};
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
take(1)({ a: 1, b: 2 });
take(1)([1, 2]);
take(1)(new Set([1, 2]));
take(1)(
new Map([
['a', 1],
['b', 2],
]),
);
take(1)('ab');
Drop
function drop(limit) {
return function (iterable) {
};
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
drop(1)({ a: 1, b: 2 });
drop(1)([1, 2]);
drop(1)(new Set([1, 2]));
drop(1)(
new Map([
['a', 1],
['b', 2],
]),
);
drop(1)('ab');
Find
function find(callback[, thisArg]) {
return function (iterable) {
}
}
function callback(currentValue[, index[, iterable]]) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
find(x => x % 2)({ a: 1, b: 2 });
find(x => x % 2)([1, 2]);
find(x => x % 2)(new Set([1, 2]));
find(x => x % 2)(
new Map([
['a', 1],
['b', 2],
]),
);
find(x => x % 2)('12');
Every
function every(callback[, thisArg]) {
return function (iterable) {
}
}
function callback(currentValue[, index[, iterable]]) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
every(x => x <= 2)({ a: 1, b: 2 });
every(x => x <= 2)([1, 2]);
every(x => x <= 2)(new Set([1, 2]));
every(x => x <= 2)(
new Map([
['a', 1],
['b', 2],
]),
);
every(x => x === '1')('12');
Some
function some(callback[, thisArg]) {
return function (iterable) {
}
}
function callback(currentValue[, index[, iterable]]) {
}
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
some(x => x <= 2)({ a: 1, b: 2 });
some(x => x <= 2)([1, 2]);
some(x => x <= 2)(new Set([1, 2]));
some(x => x <= 2)(
new Map([
['a', 1],
['b', 2],
]),
);
some(x => x === '1')('12');
Iterator
Iterator
allows you to chain operations more easily. It supports all these methods:
drop
take
map
filter
every
some
reduce
find
len
forEach
You can also use the method build()
to reconstruct an iterable (either from the same type as the input, or you can transform it).
Supported iterables
- array
- string
- Map
- Set
- Object
- iterators
Examples
const iterable = Iterable.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
iterable
.drop(1)
.filter(x => x % 2 === 0)
.map(x => x * 3)
.take(2)
.build();
build()
accepts an optional parameter type
:
const iterable = Iterable.from([1, 2, 3]);
iterable.build(type.Set);