728x90
반응형
reduce 만들기
function _reduce(list, iter, memo){
_each(list, function(val){
memo = iter(memo, val);
});
return memo;
}
console.log(_reduce([1, 2, 3], add, 0));
memo(초기값) 이 설정이 되어있지 않는경우
//array like Object를 위한 함수 만들기
var slice = Araay.prototype.slice;
function _rest(list, num){
return slice.call(list, num || 1 );
}
function _reduce(list, iter, memo){
if( arguments.length === 2 ){
memo = list[0];
list = _rest(list);
}
_each(list, function(val){
memo = iter(memo, val);
});
}
파이프라인 만들기
1. _pipe - 함수들을 받아 함수를 연속으로 실행 해줄 함수를 return
// _pipe 는 함수를 받아서 이 함수를 연속적으로 실행해주는 함수를 return 해주는 함수이다.
function _pipe(){
var fns = arguments;
return function(arg){
return _reduce(fns,funciton(arg, fn){
return fn(arg);
}, arg);
}
}
var f1 = _pipe(
funciton(a){ return a + 1;},
funciton(a){ return a *2; }
);
f1(1);//1 +1, 2*2 => return 4
//pipe의 보다 추상화된 level의 함수가 reduce이다.
2. _go - 인자, 함수를 받아 결과를 return 해주는 함수
function _go(){
var fns = _rest(arguments);
return _pipe.apply(null, fns)(arg);
}
_go(1,
function(a){ return a+1;},
function(a){ return a*2;},
function(a){ return a *a},
console.log);
3. users에 _go 적용
console.log(
_map(_filter(users, function(users){return user.age >=30}),_get('name'));
_go(users,
funciton(users){
return _filter(users, function(){
return user.age >=30;
});
},
function(users){
return _map(usrs, _get('name'));
},
console.log
);
//curryr을 이용하여 코드를 간결히 나타낼 수 있다.
var _map = _curryr(_map),
_filter = _curryr(_filter);
console.log(_map[1, 2, 3], funciton(val){return val*2}));
console.log(function(val){return val*2})([1,2,3]));
//위 두개가 같은 결과이다.
_go(users,
_filter(function(user){return user.age >= 30;}),
_map(_get('name')),
console.log);
_go(users,
_filter( user => user.age < 30),
_map(_get('age')),
console.log);
추상화의 단위가 함수..
수업듣다가 너무 어려워서 울뻔했다 ㅠㅠ..나는 멍청이 흑흑..
차분하게 복습하기 ㅠㅠㅠㅠㅠ
반응형
'창고(2021년 이전)' 카테고리의 다른 글
[JS] Symbol : ES6 type (0) | 2019.11.04 |
---|---|
[CSS Rendering] CSSOM & Vendor Prefix1 (0) | 2019.11.03 |
[JS] 함수형 프로그래밍 - 2-1_함수형으로 전환하기 (0) | 2019.10.30 |
JS시계 API - flip clock을 이용한 출퇴근 관리 (0) | 2019.10.29 |
[CSS Rendering] Box Model & Absolute Position (0) | 2019.10.29 |