FxJS - Functional Extensions for Javascript
ES6+에서 사용하는 함수형 라이브러리
설치
npm i fxjs2
목차
map
map(a => a + 10, [1, 2, 3]);
filter
filter(a => a % 2, [1, 2, 3]);
reduce
const add = (a, b) => a + b
reduce(add, [1, 2, 3]);
reduce(add, 10, [1, 2, 3]);
reduce(add, {a: 1, b: 2, c: 3});
await reduce(add, [Promise.resolve(1), 2, 3])
take
take(1, [1, 2, 3]);
take(2, [1, 2, 3])
L.map
const lazy = L.map(a => a + 10, [1, 2, 3]);
take(2, lazy);
L.filter
const lazy = L.filter(a => a % 2, [1, 2, 3, 4, 5]);
take(2, lazy);
const lazy = L.filter(a => a % 2, L.map(a => a + 10, [1, 2, 3, 4, 5]));
take(2, lazy);
go
const b = go(
0,
a => a + 1,
a => a + 10,
a => a + 100);
console.log(b);
try {
const b = go(
0,
a => { throw { hi: 'ho' } },
a => a + 10,
a => a + 100);
console.log(b);
} catch (c) {
console.log(c);
}
const b = await go(
0,
a => Promise.resolve(a + 1),
a => a + 10,
a => a + 100);
console.log(b);
try {
const b = await go(
0,
a => Promise.resolve(a + 1),
a => Promise.reject({ hi: 'ho' }),
a => a + 100);
console.log(b);
} catch (c) {
console.log(c);
}
try {
const b = await go(
0,
a => Promise.resolve(a + 1),
a => Promise.reject({ hi: 'ho' }),
a => a + 100);
console.log(b);
} catch (c) {
console.log(c);
}