This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 세 번째 인자에 `filter(selected)`를 전달하여 선택된 상품의 수량만 합산 | |
console.log( pdts.total(quantity, products, filter(selected)) ); // 7 | |
pdts.selected = { | |
// pdts.total의 before에 ...filter(selected) 추가 | |
quantity: _ => pdts.total(quantity, _, filter(selected)), | |
price: _ => pdts.total(pdt.totalPrice, _, filter(selected)), | |
// pipe로도 확장 가능 | |
discountedPrice: pipe(filter(selected), pdts.discountedPrice) | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const products = [ | |
{ id: 1, selected: true, name: 'A 반팔티', price: 12000, discount: 2000, quantity: 3 }, | |
{ id: 2, selected: false, name: 'B 후드티', price: 30000, discount: 0, quantity: 3 }, | |
{ id: 3, selected: true, name: 'C 폰케이스', price: 20000, discount: 0, quantity: 2 }, | |
{ id: 4, selected: false, name: 'D 반팔티', price: 12000, discount: 2000, quantity: 4 }, | |
{ id: 5, selected: true, name: 'E 쿠션', price: 25000, discount: 5000, quantity: 2 } | |
]; | |
// 네임 스페이스 - 상품들에 대한 함수들 | |
const pdts = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const add = (a, b) => a + b; | |
const sub = (a, b) => a - b; | |
const mult = (a, b) => a * b; | |
const baseCalcF = calc => (...fs) => a => go( | |
map(f => f(a), fs), | |
reduce(calc)); | |
// 5 + 10, 5 + 20 | |
baseCalcF(add)(a => a + 10, a => a + 20)(5); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const p1 = { | |
id: 1, | |
selected: true, | |
name: 'A 반팔티', | |
price: 12000, | |
discount: 2000, | |
quantity: 3 | |
}; | |
// map에 함수를 하나만 전달하여 자동 부분 적용 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
const url = require('url'); | |
const querystring = require('querystring'); | |
const getBody = req => new Promise(function(resolve) { | |
let body = ''; | |
req.on('data', data => body += data); | |
req.on('end', _ => resolve( | |
req.headers['content-type'] === 'application/json' ? | |
JSON.parse(body) : querystring.parse(body) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { map, some } = Functional; | |
const f13 = pipe( | |
map(a => new Promise(function(resolve) { | |
resolve(a + 5); | |
})), | |
some(a => a > 10), // <- map에서 비동기를 제어한 후 some에 [10, 15, 20] 전달 | |
console.log | |
).error( | |
e => console.log('err --->', e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const f12 = pipe( | |
arr => arr.map(a => new Promise(function(resolve, reject) { | |
reject(`${a} reject!`); | |
})), | |
arr => arr.some(a => a > 10), // <-- 여기에 [Promise, Promise, Promise] 가 내려가고 | |
console.log // <-- false가 찍힙니다. | |
).error( | |
e => console.log('err --->', e) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const f10 = pipe( | |
arr => arr.map(a => a + 5), | |
arr => arr.some(a => a > 10), | |
console.log | |
).error( | |
e => console.log('err --->', e) | |
); | |
f10([5, 10, 15]); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipe( | |
a => { throw 'ho' }, | |
a => 10 | |
).error( | |
_ => console.log('에러!') | |
).complete( | |
a => console.log('성공 - ', a) | |
) (); | |
// 에러! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pipe( | |
a => asdasdasd, | |
a => 10 | |
) | |
.error(e => match(e) | |
.case(e => e instanceof ReferenceError) ( | |
_ => console.log('ref 에러!') | |
).else ( | |
_ => console.log('에러!') | |
) |