Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

유인동 indongyoo

🎯
Focusing
View GitHub Profile
@indongyoo
indongyoo / solution.js
Last active May 28, 2020
김동현님 질문 코드
View solution.js
function* filter(f, iter) {
for (const a of iter) if (f(a)) yield a;
}
function* take(limit, iter) {
for (const a of iter) if (limit--) yield a;
}
const head = iter => take(1, iter).next().value;
View map, flatMap, then.js
const log = a => console.log(a);
// 0.
// map :: Functor f => (a -> b) -> f a -> f b
// 1. Array.prototype.map
// Array r => (a -> b) -> r a -> r b
// [1] : r a
// a => a + 1 : (a -> b)
View mapObject.js
function *entriesLazy(obj) {
for (const k in obj) yield [k, obj[k]];
}
[...entriesLazy({ a: 1, b: 2 })];
// [['a', 1], ['b', 2]]
function *mapEntriesLazy(f, iter) {
for (const [k, a] of iter) yield [k, f(a)];
}
View series, concurrency 1.js
// series
const posts = await getPosts();
const users = await getUsers();
// concurrency
const [posts, users] = await Promise.all([
getPosts(),
getUsers()
]);
View flat+deepFlat.js
const isIterable = a => !!(a && a[Symbol.iterator]);
function *flat(iter) {
for (const a of iter) {
if (isIterable(a)) yield *a;
else yield a;
}
}
function *deepFlat(iter) {
View infinity+takeWhile.js
function f1(max) {
let i = -1, total = 0;
while (true) {
let a = ++i;
a = a * a;
if (!(a < max)) break;
total += a;
}
return total;
}
View flatten+flatMap.js
const log = console.log;
const isIterable = a => !!(a && a[Symbol.iterator]);
function *flatten(iter) {
for (const a of iter) {
if (isIterable(a)) for (const b of a) yield b;
else yield a;
}
}
View gen+map+filter+reduce.js
function reduce(f, acc, iter) {
if (arguments.length == 2) {
iter = acc[Symbol.iterator]();
acc = iter.next().value;
}
for (const a of iter) acc = f(acc, a);
return acc;
}
function *map(f, iter) {
View reduce+generator.js
function reduce(f, acc, iter) {
if (arguments.length == 2) {
iter = acc[Symbol.iterator]();
acc = iter.next().value;
}
for (const a of iter) acc = f(acc, a);
return acc;
}
reduce((a, b) => a + b, function*() {
View fnes_nc_dq.js
// 선택된 모든 상품 중 할인 금액이 있는 상품의 총 수량 - pipe 이용
pdts.selected.dq = pipe(
filter(selected),
filter(discount),
_ => pdts.total(quantity, _));
console.log( pdts.selected.dq(products) ); // 5
// 선택된 모든 상품 중 할인 금액이 있는 상품의 총 수량 - pipe + and 이용
pdts.selected.dq = pipe(