ํจ์ํ ๊ฐ์ ์น์ 7 ๋ด์ฉ ์ ๋ฆฌ - 1
(๊ฐ์๋ช : ํจ์ํ ํ๋ก๊ทธ๋๋ฐ๊ณผ JavaScript ES6+ ์ ์ธ๋ ๊ฐ์ฌ๋)
Array.prototype.join์ ๋ฐฐ์ด๋ง ๊ฐ๋ฅํ ํจ์์ธ๋ฐ Array.prototype.join๋ณด๋ค ๋ ๋ค์์ฑ์ด ๋์ joinํจ์๋ฅผ ๋ง๋ค์ด๋ณด์.
// 1.
// joinํจ์๋ ๊ธฐ๋ณธ ๊ตฌ๋ถ์๊ฐ ','
const join = (sep=',', iter) =>
reduce((a, b) => `${a}${sep}${b}`, iter); //sep(์ธํผ๋ ์ดํฐ)์ a์ b์ฌ์ด์ ๋ฃ์ด์ค
// 2.
// ํ์ดํ๋ผ์ธ์์ ์กฐํฉ์ฑ์ด ์ข๊ฒ currry ์ ์ฉ
const join = curry((sep=',', iter) =>
reduce((a, b) => `${a}${sep}${b}`, iter);
const queryStr = pipe(
Object.entries,
map(([k, v]) => `${k}=${v}`),
join('&')
);
log(queryStr({limit: 10, offset: 10, type: 'notice'}));
// limit=10&offset=10&type=noticeํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ ํ๋ฉด ์ฌ์ด์ฌ์ด์ ์๋ ํจ์๋ค์ ๊บผ๋ด์ ์กฐํฉ์ฑ, ์ฌ์ฌ์ฉ์ฑ์ด ๋๊ฒ ํ๋ก๊ทธ๋๋ฐ์ ํ ์ ์๋ค.
find๋ ์ฒซ๋ฒ์งธ๋ก ๋ง๋ ํ๋์ ๊ฐ์ ๊บผ๋ด์ฃผ๋ ํจ์ take๋ฅผ ์ด์ฉํด์ ๊ฒฐ๋ก ์ ๋ง๋๋ ํจ์๋ค.(take๋ฅผ ํตํด ์ดํฐ๋ฌ๋ธ์ ๋ฐ๋ ํจ์)
const users = [
{age: 32},
{age: 31},
{age: 37},
{age: 28},
{age: 25},
{age: 32},
{age: 31},
{age: 37}
];
// curry ์ปค๋ง์ ์ฉ
const find = curry((f, iter) => go(
iter,
L.filter(f),
take(1),
([a]) => a));
log(find(u => u.age < 30)(users));
go(users,
L.map(u => u.age),// L.map์ ์ง์ฐ์ ์ธ map
find(n => n < 30),
log);find๊ฐ ๋ฐ๋ ๊ฐ์ด ์ดํฐ๋ฌ๋ธ์ด๊ธฐ ๋๋ฌธ์ ์ฐ์ฐ์ ๋ค ์๋ฃํ ๊ฐ์ ์ค๋ ์ฌ์ฉ์ด ๊ฐ๋ฅํ๊ณ ํ๊ฐ์ง์ฐ์ ์ํจ ๊ฐ์ ์ค๋ ์ฌ์ฉ๊ฐ๋ฅํ๋ค.
go(users,
map(u => u.age),// ์๋ฃ๋ ๊ฐ์ ์ค๋
find(n => n < 30),
log);
go(users,
L.map(u => u.age),// L.map์ผ๋ก ์ง์ฐ์ ์ธ map์ ์ค๋
find(n => n < 30),
log);
//find๋ L.filter๋ฅผ ํตํด ์ง์ฐํด๋๊ณ take๋ก ์ฐ์ฐํ๋ฏ๋ก ์ด๋ค๊ฐ์ค๋ ๊ฐ๋ฅ
const find = curry((f, iter) => go(
iter,
L.filter(f),
take(1),
([a]) => a));