전체 글

import { Dream } from "future";
· Frontend/JS
L.flatten 함수 flatten이라는 함수를 만들고자 한다. 해당 함수는 임의로 배열이 섞여 있는 배열을 하나의 배열로 전환해주는 함수다. 또한, 지연 평가를 적용할 수 있는 함수다. 예를 들어, 아래와 같은 배열이 있다고 한다면 [[1, 2], 3, 4, [5, 6], [7, 8, 9]]; 이를 하나의 배열로 처리해 아래와 같이 만들어 줄 수 있다. [1, 2, 3, 4, 5, 6, 7, 8, 9] 이제 본격적으로 함수를 생성한다. const isIterable = a => a && a[Symole.iterator]; L.flatten = function *(iter) { for (const a of iter) { if (isIterable(a)) {} for (const b of a) yield..
· Frontend/JS
L.map으로 map 만들기 이터러블 객체를 순회하면서 일괄적으로 함수를 적용하는 map 함수를 지연평가를 적용한 L.map을 통해서 구현할 수 있다. 먼저 L.map의 코드는 아래와 같다. L.map = curry(function *(f, iter) { iter = iter[Symbol.iterator](); let cur; while (!(cur = iter.next()).done) { const a = cur.value; yield f(a); } }); 그리고 map 코드는 아래와 같다. const map = curry((f, iter) => { let res = []; iter = iter[Symbol.iterator](); let cur; while (!(cur = iter.next()).don..
· Frontend/JS
앞선 강의 내용 L.entries = function *(obj) { for (const k in obj) yield [k, obj[k]]; }; const join = curry((sep = ',', iter) => reduce((a, b) => `${a}${sep}${b}`, iter)); const queryStr = pipe( L.entries, L.map(([k, v]) => `${k}=${v}`), join('&') ); console.log(queryStr({ limit: 10, offset: 10, type: 'notice' })); 앞서 배웠던 join 함수는 reduce 계열의 함수라고 볼 수 있다. 즉, reduce로 만들 수 있는 함수인 것이다. entries의 경우는 map을 통해..
· Frontend/JS
이전 강의 내용 const queryStr = pipe( Object.entries, map(([k, v]) => `${k}=${v}`), reduce((a, b) => `${a}&${b}`) ); reduce를 보면 Array에 있는 join 함수와 같은 결과물을 반환한다. 그런데 Array의 join 메서드는 Array prototype에만 붙어있는 메서드다. 이때, reduce는 이터러블 객체를 순회하면서 값을 join하기에 조금 더 다형성이 높은 함수라고 볼 수 있다. Join 함수 const join = curry((sep = ',', iter) => reduce((a, b) => `${a}${sep}${b}`, iter)); const queryStr = pipe( Object.entries,..
턴태
턴태의 밑바닥부터 시작하는 de-vlog