Skip to content

Instantly share code, notes, and snippets.

View indongyoo's full-sized avatar
🎯
Focusing

유인동 indongyoo

🎯
Focusing
View GitHub Profile
@indongyoo
indongyoo / solution.js
Last active May 28, 2020 09:46
김동현님 질문 코드
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;
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)
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)];
}
// series
const posts = await getPosts();
const users = await getUsers();
// concurrency
const [posts, users] = await Promise.all([
getPosts(),
getUsers()
]);
function f1(max) {
let i = -1, total = 0;
while (true) {
let a = ++i;
a = a * a;
if (!(a < max)) break;
total += a;
}
return total;
}
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) {
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;
}
}
@indongyoo
indongyoo / reduce+generator.js
Last active December 6, 2018 07:44
reduce+generator
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*() {
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) {
// 선택된 모든 상품 중 할인 금액이 있는 상품의 총 수량 - 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(