FxJS - Functional Extensions for Javascript
FxJS는 자바스크립트의 기본 값을 이용하고, 이터러블 프로그래밍과 Promise를 강조한 함수형 프로그래밍 라이브러리입니다.
iterable
const res = go(
L.range(Infinity),
L.filter(a => a % 2),
L.take(3),
reduce(add));
log(res);
iterable + time
go(
L.range(Infinity),
L.map(delay(1000)),
L.map(a => a + 10),
L.take(3),
each(log));
iterable + time + Promise
(async () => {
await go(
L.interval(1000),
L.map(a => a + 30),
L.takeUntil(a => a == 33),
each(log));
const res = await go(
L.interval(1000),
L.map(a => a + 20),
L.takeWhile(a => a < 23),
L.map(tap(log)),
reduce(add));
log(res);
} ());
설치
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 iterator = L.map(a => a + 10, [1, 2, 3]);
take(2, iterator);
L.filter
const iterator = L.filter(a => a % 2, [1, 2, 3, 4, 5]);
take(2, iterator);
const iterator = L.filter(a => a % 2, L.map(a => a + 10, [1, 2, 3, 4, 5]));
take(2, iterator);
go + try catch + 비동기 에러 핸들링
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);
}
stop
const f1 = pipeS(
a => a % 2 ? stop(a) : a,
a => a + 10);
f1(1);
f1(2);
goS({a: 1, b: 2},
stopIf({a: 1}),
({a, b}) => ({a: a + 10, b}));
goS({a: 2, b: 2},
stopIf({a: 1}),
({a, b}) => ({a: a + 10, b}));
goS({a: 1, b: 2},
stopIf({a: 1}, null),
({a, b}) => ({a: a + 10, b}));