(f, iterable) => f(...iterable)
- source
const args = [10, 20];
add(...args); // 30
apply(add, args); // 30
apply(add)(args); // 30
(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
calls([(a) => a + 1, (a) => a + 2], 10);
// [11, 12]
calls(
{
a: (a) => a + 1,
b: (a) => a + 2,
},
10
);
// {a: 11, b: 12}
calls([
(_) => Promise.resolve(1),
(_) => Promise.resolve(2),
(_) => Promise.resolve(3),
]).then(log);
// [1, 2, 3]
calls({
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!
(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
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
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}]
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"}
(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]
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
minBy((a) => a * -1, [1, 3, 7, 4]);
// 7
object([
["a", 1],
["b", 2],
["c", 3],
]);
// {a: 1, b: 2, c: 3}
omit(["a, c"], { a: 1, b: 2, c: 3, d: 4 });
// {b: 2, d: 4}
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}
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]
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'}]
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