(f, iterable) => f(...iterable)
- source
const args = [10, 20];
add(...args); // 30
apply(add, args); // 30
apply(add)(args); // 30
([f], args) => [f(...args)]
({k: f}, args) => {k: f(...args)}
- source
const args = [2, 3, 4];
const addAll = (a, b, c) => a + b + c;
const multiplyAll = (a, b, c) => a * b * c;
addAll(...args) // 9
multiplyAll(...args) // 24
applyEach([addAll, multiplyAll], args) // [9, 24]
applyEach({ add: addAll, mul: multiplyAll }, args) // { add: 9, mul: 24 }
(String k, { k: v }, args) => v(...args)
String k => ({ k: v }, args) => v(...args)
- source
applyMethod('add', { add: (a, b, c = 0) => a + b + c }, [1, 2]) // 3
applyMethod('add', { add: (a, b, c = 0) => a + b + c }, [1, 2, 3]) // 6
applyMethod('add')({ add: (a, b, c = 0) => a + b + c }, [1, 2, 3]) // 6
(f, ...args) => f(...args)
- source
add(10, 20); // 30
call(add, 10, 20); // 30
call(add)(10, 20); // 30
([(a, b) => c, (a, b) => d, ...], a, b) => [c, d, ...]
([(a, b) => Promise c, (a, b) => Promise d, ...], a, b) => Promise [c, d]
({ k: (a, b) => c, k2: (a, b) => d }, a, b) => { k: c, k2: d }
({ k: (a, b) => Promise c, k2: (a, b) => Promise d }, a, b) => Promise { k: c, k2: d }
- source
callEach([(a) => a + 1, (a) => a + 2], 10);
// [11, 12]
callEach(
{
a: (a) => a + 1,
b: (a) => a + 2,
},
10
);
// {a: 11, b: 12}
callEach([
(_) => Promise.resolve(1),
(_) => Promise.resolve(2),
(_) => Promise.resolve(3),
]).then(log);
// [1, 2, 3]
callEach({
a: (_) => Promise.resolve(1),
b: (_) => Promise.resolve(2),
c: (_) => Promise.resolve(3),
}).then(log);
// {a: 1, b: 2, c: 3}
const a = constant("A");
a(); // A
a(); // A
((a, b, ...) => e) => a => (b, ...) => e
((a, b, ...) => e) => (a, b, ...) => e
- source
const add = curry((a, b) => a + b);
const add10 = add(10);
add10(5); // 15
add10(6); // 16
add(10, 5); // 15
const addAll = (...args) => args.reduce((a, b) => a + b);
const add1 = curryN(1, addAll);
log(add1(1)(2)); // 3
log(add1(1, 2)); // 3
// add1(1)(2)(3) => error!
const add2 = curryN(2, addAll);
log(add2(1)(2)(3)); // 6
log(add2(1, 2)(3)); // 6
log(add2(1)(2, 3)); // 6
log(add2(1, 2, 3)); // 6
// add2(1)(2)(3)(4); => error!
(f, time) => (...args) =>
- source
(a, a => b, b => c, ..., y => z) => z
(Promise a, a => b, b => c, ..., y => z) => Promise z
(a, a => Promise b, b => Promise c, ..., y => z) => Promise z
- source
go(
0,
(a) => a + 1,
(a) => a + 10,
log
); // 11
go(
0,
(a) => Promise.resolve(a + 1),
(a) => a + 10,
log
); // 11
const b = go(
0,
(a) => a + 1,
(a) => a + 10
);
log(b); // 11
const pb = go(
0,
(a) => Promise.resolve(a + 1),
(a) => a + 10
);
pb.then(log); // 11
(f, f2, f3, ...) => (...args) => [f(...args), f2(...args), f3(...args), ...]
- source
const compute = juxt(min, max, sum, mean);
log(...compute([1, 2, 3, 4, 5])); // 1, 5, 15, 3
const a = negate((a) => a);
log(a(true)); // false
log(a(false)); // true
const f = once((a) => a + 10);
log(f(5)); // 15
log(f(5)); // 15
((a, b, ...) => d, d => e, ..., y => z) => (a, b, ...) => z
((a, b, ...) => Promise d, d => e, e => Promise f, ..., y => z) => (a, b, ...) => Promise z
- source
const f1 = pipe(
(a) => a.toUpperCase(),
(a) => a == "A"
);
const b = f1("a");
log(b); // true
const total = (f) =>
pipe(
map(f),
reduce((a, b) => a + b)
);
const totalAge = total(({ age }) => age);
go(fetchUsers(), totalAge, log);
// 186
go(
fetchProducts(),
total(({ price }) => price),
log
);
// 156000
(g, f, ...) => a => go(a, g, f, ..., _ => a)
- source
go(
10,
(a) => a + 5,
tap((a) => a + 5, log), // 20
(a) => a + 10,
log
); // 25
Number => Number => Number
- source
add(10, 5);
// 15
add(10)(5);
// 15
const sel = baseSel(".");
sel("a.b", { a: { b: 10 } });
// 10
sel("a.b", { b: { c: 20 } });
// undefined
const sel2 = baseSel(">");
sel2("a>b", { a: { b: 10 } });
// 10
sel2("a>b", { b: { c: 20 } });
// undefined
// useful for functions to block multiple calls
const callApi = blockUntilSettled(() => {
console.log('called!');
return delay(100, 5);
});
(async function () {
const p1 = callApi(); // console - called!
console.log('after p1'); // console - after p1
const p2 = callApi(); //
console.log('after p2'); // console - after p2
const p3 = callApi(); //
console.log('after p3'); // console - after p3
const [r1, r2, r3] = await Promise.all([p1, p2, p3]);
const r4 = await callApi(); // console - called!
console.log(r1, r2, r3, r4); // 5, undefined, undefined, 5
})();
chunk(2, [1, 2, 3, 4, 5]);
// [[1, 2], [3, 4], [5]]
Iterable a => [a]
Iterable Promise a => Promise [a]
- source
compact([1, 2, 0, false, true, null]);
// [1, 2, true]
(a => b) => Iterable a => { [b]: n }
(a => b) => Iterable Promise a => Promise { [b]: n }
(a => Promise b) => Iterable a => Promise { [b]: n }
(a => Promise b) => Iterable Promise a => Promise { [b]: n }
- source
countBy((a) => (a % 2 ? "odd" : "even"), [1, 2, 3, 4, 5]);
// { odd: 3, even: 2 }
[[[[a]]]] => [a]
Iterable Iterable Iterable ... Iterable a => [a]
[Promise [[Promise a]]] => Promise [a]
[Promise [[Iterable Promise a]]] => Promise [a]
- source
deepFlat([[1, 2, [3, [4, 5, [6], [[7]]]]]]);
// [1, 2, 3, 4, 5, 6, 7];
({}, {}, ..., {}) => {}
- source
defaults({ flavor: "chocolate" }, { flavor: "vanilla", sprinkles: "lots" });
// {flavor: "chocolate", sprinkles: "lots"}
const obj = { a: 1, c: null, d: NaN };
defaultTo(0, obj.a);
// 1
defaultTo(0, obj.b);
// 0
defaultTo(0, obj.c);
// 0
defaultTo(0, obj.d);
// 0
time => a => Promise a
(time, a) => Promise a
- source
go("hi", delay(1000), log); // After 1 second "hi"
difference([2, 3], [2, 1]);
// [1]
difference([2, 2, 2, 2], [1, 1, 1, 1, 1]);
// [1, 1, 1, 1, 1]
difference([1, 2, 3, 4], [1]);
// []
difference([2], [1, 2, 3, 4]);
// [1, 3, 4]
differenceBy((a) => a.x, [{ x: 1 }], [{ x: 2 }, { x: 1 }]);
// [{ x: 2 }]
const cmp = (x, y) => x.a === y.a;
const l1 = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }];
const l2 = [{ a: 3 }, { a: 4 }];
differenceWith(cmp, l1, l2);
// [{a: 1}, {a: 2}, {a: 5}]
divide(4, 2) // 2
divide(4)(2) // 2
drop([1, 2, 3, 4]);
// [2, 3, 4]
dropRight([1, 2, 3, 4]);
// [1, 2, 3]
dropUntil((a) => a > 1, [1, 2, 3, 4]);
// [3, 4]
dropWhile((a) => a < 3, [1, 2, 3, 4]);
// [3, 4]
(a => b) => Iterable a => [a]
(a => b) => Iterable Promise a => Promise [a]
(a => Promise b) => Iterable a => Promise [a]
(a => Promise b) => Iterable Promise a => Promise [a]
- source
go(
document.querySelectorAll(".post"),
each((el) => (el.innerHTML = "")),
log
); // [div.post, div.post, div.post];
entries({ a: 1, b: 2, c: 3 });
// [['a', 1], ['b', 2], ['c', 3]]
({}, {}, ..., {}) => {}
- source
extend({ flavor: "vanilla", sprinkles: "lots" }, { flavor: "chocolate" });
// {flavor: "chocolate", sprinkles: "lots"}
({}, {}, ..., {}) => {}
- source
extendRight({ flavor: "vanilla", sprinkles: "lots" }, { flavor: "chocolate" });
// {flavor: "vanilla", sprinkles: "lots"}
(a => Boolean) => Iterable a => [a]
(a => Boolean) => Iterable Promise a => Promise [a]
(a => Promise Boolean) => Iterable a => Promise [a]
(a => Promise Boolean) => Iterable Promise a => Promise [a]
- source
filter((a) => a % 2, [1, 2, 3]);
// [1, 3]
filter((a) => a % 2, [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(log);
// [1, 3]
filter((a) => Promise.resolve(a % 2), [1, 2, 3]).then(log);
// [1, 3]
filter((a) => Promise.resolve(a % 2), [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(log);
// [1, 3]
find = head . L.filter
(a => Boolean) => Iterable a => a
(a => Promise Boolean) => Iterable a => Promise a
(a => Boolean) => Iterable Promise a => Promise a
(a => Promise Boolean) => Iterable Promise a => Promise a
- source
find(a => a > 3, [1, 2, 3, 4, 5]);
// 4
find(({age}) => age == 23, [
{ name: 'a', age: 15, ... },
{ name: 'b', age: 19, ... },
{ name: 'c', age: 23, ... },
{ name: 'd', age: 17, ... },
{ name: 'e', age: 23, ... }
]);
// { name: 'c', age: 23, ... }
{k: v} => Iterable {k: v} => {k: v}
{k: v} => Iterable Promise {k: v} => Promise {k: v}
- source
findWhere({ age: 23 }, [
{ name: 'a', age: 15, ... },
{ name: 'b', age: 19, ... },
{ name: 'c', age: 23, ... },
{ name: 'd', age: 17, ... },
{ name: 'e', age: 23, ... }
]);
// { name: 'c', age: 23, ... }
findWhere({ name: 'e', age: 23 }, [
{ name: 'a', age: 15, ... },
{ name: 'b', age: 19, ... },
{ name: 'c', age: 23, ... },
{ name: 'd', age: 17, ... },
{ name: 'e', age: 23, ... }
]);
// { name: 'e', age: 23, ... }
(Iterable Iterable a, Number depth) => [a]
(Iterable Promise Iterable a, Number depth) => Promise [a]
(Iterable Iterable Promise a, Number depth) => Promise [a]
- source
flat([[1, 2], [3, 4]]);
// [1, 2, 3, 4]
flat([[1, [2]],[[[3]]]]);
// [1, [2], [[3]]];
flat([[1, [2]], [[[3]]]], 2);
// [1, 2, [3]];
flat([[1, [2]], [[[3]]]], 3);
// [1, 2, 3];
await flat([Promise.resolve([1, 2]), [Promise.resolve(3), 4]]);
// [1, 2, 3, 4]
flatMap = flat . mapLazy
(a => Iterable b) => Iterable a => [b]
(a => Iterable b) => Iterable Promise a => Promise [b]
(a => Iterable Promise b) => Iterable a => Promise [b]
(a => Promise Iterable b) => Iterable Promise a => Promise [b]
(a => Promise Iterable Promise b) => Iterable a => Promise [b]
(a => Promise Iterable Promise b) => Iterable Promise a => Promise [b]
- source
flatMap((a) => range(a), [1, 2]);
// [0, 0, 1]
await flatMap((a) => Promise.resolve(range(a)), [1, 2]);
// [0, 0, 1]
groupBy((a) => (a % 2 ? "odd" : "even"), [1, 2, 3, 4, 5]);
// { odd: [1, 3, 5], even: [2, 4] }
const identity = (a) => a;
const products = [
{ id: 1, price: 100 },
{ id: 3, price: 100 },
{ id: 5, price: 100 },
];
indexBy((p) => p.id, products);
// {1: {id: 1, price: 100}, 3: {id: 3, price: 100}, 5: {id: 5, price: 100}}
initial([1, 2, 3]);
// [1, 2]
(Number a, b, Array c) => Array d
- source
// prepend
insert(-1, 0, [1, 2, 3]) // [0, 1, 2, 3]
insert(0, 0, [1, 2, 3]) // [0, 1, 2, 3]
// middle
insert(2, 2.5, [1, 2, 3]) // [1, 2, 2.5, 3]
// append
insert(3, 4, [1, 2, 3]) // [1, 2, 3, 4]
insert(100, 4, [1, 2, 3]) // [1, 2, 3, 4]
intersection([2, 1], [2, 3]);
// [2]
intersection([1, 2, 1, 1, 3], [1, 1, 1, 2, 4]);
// [1, 2]
intersectionBy((o) => o.x, [{ x: 2 }, { x: 1 }], [{ x: 1 }]);
// [{ x: 1 }]
const cmp = (x, y) => x.a === y.a;
const l1 = [{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }, { a: 5 }];
const l2 = [{ a: 3 }, { a: 4 }];
intersectionWith(cmp, l1, l2);
// [{a: 3}, {a: 4}]
keys({ a: 1, b: 2, c: 3 });
// ['a', 'b', 'c']
(a => b) => Iterable a => [b]
(a => b) => Iterable Promise a => Promise [b]
(a => Promise b) => Iterable a => Promise [b]
(a => Promise b) => Iterable Promise a => Promise [b]
- source
map((a) => a + 10, [1, 2, 3]);
// [11, 12, 13]
map((a) => Promise.resolve(a + 10), [1, 2, 3]).then(log);
// [11, 12, 13]
map((a) => a.nodeName, document.querySelectorAll("head *"));
// ["META", "TITLE", "SCRIPT"]
map(
(a) => a + 10,
(function* () {
yield 4;
yield 5;
})()
);
// [14, 15]
(a => b) => Iterable [k, a] => [[k, b]]
(a => b) => Iterable [k, Promise a] => Promise [[k, b]]
(a => Promise b) => Iterable [k, a] => Promise [[k, b]]
(a => Promise b) => Iterable [k, Promise a] => Promise [[k, b]]
- source
mapEntries((a) => a + 10, [
["a", 1],
["b", 2],
]);
// [['a', 11], ['b', 12]]
mapEntries((a) => Promise.resolve(a + 10), [
["a", 1],
["b", 2],
]).then(log);
// [['a', 11], ['b', 12]]
// entries == Object.entries
// object == Object.fromEntries
object(mapEntries((a) => a + 10, entries({ a: 1, b: 2 })));
// { a: 11, b: 12 }
go(
{ a: 1, b: 2 },
entries,
mapEntries((a) => Promise.resolve(a + 10)),
object
).then(log);
// { a: 11, b: 12 }
(a => b) => { k: a } => { k: b }
(a => b) => { k: Promise a } => Promise { k: b }
(a => Promise b) => { k: a } => Promise { k: b }
(a => Promise b) => { k: Promise a } => Promise { k: b }
- source
mapObject((a) => a + 10, { a: 1, b: 2 });
// { a: 11, b: 12 }
mapObject((a) => Promise.resolve(a + 10), { a: 1, b: 2 }).then(log);
// { a: 11, b: 12 }
go(
{ a: 1, b: 2 },
mapObject((a) => Promise.resolve(a + 10)),
log
);
// { a: 11, b: 12 }
maxBy((a) => a * -1, [1, 3, 7, 4]);
// 1
mean([1, 2, 3, 4, 5, 6]) // 3
mean(L.range(1, 6)) // 3
await mean(Promise.resolve([1, 2, 3, 4, 5, 6])) // 3
meanBy(identity, [1, 2, 3, 4, 5, 6]) // 3
meanBy(
sel('age'),
[{ age: 10 }, { age: 20 }, { age: 30 }, { age: 40 }, { age: 50 }, { age: 60 }]
)
// 35
minBy((a) => a * -1, [1, 3, 7, 4]);
// 7
object([
["a", 1],
["b", 2],
["c", 3],
]);
// {a: 1, b: 2, c: 3}
([], {}) => {}
(keys, object) => object without input keys
- source
omit(["a, c"], { a: 1, b: 2, c: 3, d: 4 });
// {b: 2, d: 4}
omitBy <-> pickBy
(f, {}) => {}
- source
omitBy(([k]) => ['a', 'b'].includes(k), { a: 1, b: 2, c: 3, d: 4 });
// {b: 2, d: 4}
omitBy(([k, v]) => v % 2 === 0, { a: 1, b: 2, c: 3, d: 4 });
// {a: 1, c: 3}
partition((a) => a % 2, [1, 2, 3, 4, 5]);
// [[1, 3, 5], [2, 4]]
pick(["a, c"], { a: 1, b: 2, c: 3, d: 4 });
// {a: 1, c: 3}
pickBy <-> omitBy
(f, {}) => {}
- source
pickBy(([k, v]) => ['a', 'c'].includes(k), { a: 1, b: 2, c: 3, d: 4 });
// {a: 1, c: 3}
pickBy(([k, v]) => v % 2 === 0, { a: 1, b: 2, c: 3, d: 4 });
// {b: 2, d: 4}
String k => Iterable a => [a[k]]
String k => Iterable Promise a => Promise [a[k]]
- source
pluck("id", [{ id: 1 }, { id: 3 }]);
// [1, 3]
([start=0], end, [step=1]) => [Number a, ...]
- source
range(4);
// => [0, 1, 2, 3]
range(-4);
// => [0, -1, -2, -3]
range(1, 5);
// => [1, 2, 3, 4]
range(0, 20, 5);
// => [0, 5, 10, 15]
range(0, -4, -1);
// => [0, -1, -2, -3]
((a, b) => c) => Iterable a => c
((a, b) => Promise c) => Iterable a => Promise c
((a, b) => c) => Iterable Promise a => Promise c
((acc, b) => acc) => acc => Iterable b => acc
((acc, b) => Promise acc) => acc => Iterable b => Promise acc
((acc, b) => acc) => acc => Iterable Promise b => Promise acc
- source
const add = (a, b) => a + b;
reduce(add, [1, 2, 3]);
// 6
reduce(add, 10, [1, 2, 3]);
// 16
await reduce(add, [Promise.resolve(1), Promise.resolve(2), Promise.resolve(3)]);
// 6
(a => Boolean) => Iterable a => []
(a => Boolean) => Iterable Promise a => Promise []
(a => Promise Boolean) => Iterable a => Promise []
(a => Promise Boolean) => Iterable Promise a => Promise []
- source
reject((a) => a % 2, [1, 2, 3]);
// [2]
reject((a) => a % 2, [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(log);
// [2]
reject((a) => Promise.resolve(a % 2), [1, 2, 3]).then(log);
// [2]
reject((a) => Promise.resolve(a % 2), [
Promise.resolve(1),
Promise.resolve(2),
Promise.resolve(3),
]).then(log);
// [2]
(Number start, Iterable) => Array
(Number start, Number count, Iterable) => Array
- source
// remove with index
remove(0, [1, 2, 3, 4, 5]) // [1, 2, 3, 4]
remove(0, L.range(5)) // [1, 2, 3, 4]
// remove with count
remove(1, 2, [1, 2, 3, 4, 5]) // [0, 3, 4]
remove(1, 2, L.range(5)) // [0, 3, 4]
(a, Number size) => [a, a, a, ...a]
- source
repeat("hi", 5) // ["hi", "hi", "hi", "hi", "hi"]
(String substr|Regex, String newSubstr, String target) => String
- source
// search by string
replace("foo", "bar", "foo foo foo") // "bar foo foo"
// search by regex
replace(/foo/g, "bar", "foo foo foo") // "bar bar bar"
(Number, Number) => Number
- source
range . map
(f, Number n) => [f(1), f(2), ...f(n-1)]
- source
// times(String, 3)
times(String, 3) // ["0", "1", "2"]
times(n => n * 2, 5) // [0, 2, 4, 6, 8]
// times(Promise.resolve.bind(Promise), 3)
await times(n => Promise.resolve(n), 3) // [0, 1, 2]
Iterable a => [a]
Iterable a => Promise [a]
- source
unique([1, 2, 3, 1, 2, 4]);
// [1, 2, 3, 4]
(a => b) => Iterable a => [a]
(a => b) => Iterable Promise a => Promise [a]
(a => Promise b) => Iterable a => Promise [a]
(a => Promise b) => Iterable Promise a => Promise [a]
- source
const users = [
{ name: "aa" },
{ name: "Aa" },
{ name: "bb" },
{ name: "cc" },
{ name: "bb" },
];
uniqueBy((u) => u.name.toUpperCase(), users);
// [{name: 'aa'}, {name: 'bb'}, {name: 'cc'}]
isNil(undefined) // true
isNil(null) // true
isNil(false) // false
isNil([]) // false
isNil('') // false
const iterator = L.filter((a) => a % 2, [1, 2, 3]);
iterator.next(); // { value: 1, done: false }
iterator.next(); // { value: 3, done: false }
iterator.next(); // { value: undefined, done: true }
go(
L.range(1, Infinity),
L.filter((a) => a % 2),
take(2)
);
// [1, 3]
await go(
L.range(Infinity),
L.map((a) => Promise.resolve(a)),
L.filter((a) => a % 2),
take(2)
);
// [1, 3]
reduceS(
(a, b) => {
const res = a + b;
return res > 5 ? stop(res) : res;
},
[1, 2, 3, 4]
);
// 6
const f1 = pipeS(
(a) => (a % 2 ? stop(a) : a),
(a) => a + 10
);
f1(1);
// 1
f1(2);
// 12
const f2 = pipeS(
stopIf((a) => a % 2),
(a) => a + 10
);
f2(1);
// 1
f2(2);
// 12
goS({ a: 1, b: 2 }, stopIf({ a: 1 }), ({ a, b }) => ({ a: a + 10, b }));
// {a: 1, b: 2}
goS({ a: 2, b: 2 }, stopIf({ a: 1 }), ({ a, b }) => ({ a: a + 10, b }));
// {a: 12, b: 2}
goS({ a: 1, b: 2 }, stopIf({ a: 1 }, null), ({ a, b }) => ({ a: a + 10, b }));
// null