This file contains hidden or 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
| 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; |
This file contains hidden or 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 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) |
This file contains hidden or 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
| 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)]; | |
| } |
This file contains hidden or 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
| // series | |
| const posts = await getPosts(); | |
| const users = await getUsers(); | |
| // concurrency | |
| const [posts, users] = await Promise.all([ | |
| getPosts(), | |
| getUsers() | |
| ]); |
This file contains hidden or 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 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) { |
This file contains hidden or 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
| function f1(max) { | |
| let i = -1, total = 0; | |
| while (true) { | |
| let a = ++i; | |
| a = a * a; | |
| if (!(a < max)) break; | |
| total += a; | |
| } | |
| return total; | |
| } |
This file contains hidden or 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 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; | |
| } | |
| } |
This file contains hidden or 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
| 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) { |
This file contains hidden or 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
| 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*() { |
This file contains hidden or 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 ์ด์ฉ | |
| pdts.selected.dq = pipe( | |
| filter(selected), | |
| filter(discount), | |
| _ => pdts.total(quantity, _)); | |
| console.log( pdts.selected.dq(products) ); // 5 | |
| // ์ ํ๋ ๋ชจ๋ ์ํ ์ค ํ ์ธ ๊ธ์ก์ด ์๋ ์ํ์ ์ด ์๋ - pipe + and ์ด์ฉ | |
| pdts.selected.dq = pipe( |
NewerOlder