Javascript 함수형 프로그래밍 (코드 진행과정 + 변수값)

이정민·2021년 12월 16일
0

go 함수 실행


go(users,
    filter(function(user) { return user.age <= 30; }),
    map(get('name')),
    console.log);

filter 과정


// filter 실행 과정
filter(function(user) {return user.age <=30;})

1.  // go 과정(open)

['인자 값']
// fns = [filter, map, consol.log] 실제로 filter와 map은 curryr로 인해서 function(b) {return fn(b,a);} 너무 길기 때문에 filter, map으로 요약
// arg = users


pipe.apply(null,fns)(arg)      //pipe 함수 호출




2. //pipe 과정

['인자 값']
// fns = [filter, map, consol.log]
// arg = users


reduce(fns, function(arg, fn){return fn(arg);}, arg);  //reduce 함수 호출




3. // reduce 과정

['인자 값']
// list = [filter, map, console.log]
// iter = function(users,fn) {return fn(users)}
// memo = users


each(list, function(val) {memo = iter(memo, val);}); //each 함수 호출




4. // each 과정

['인자 값']
// list = [filter, map, console.log]
// iter = function(val) {memo = iter(memo, val);});


keys(list); //keys 함수 호출




5. // keys 과정

['인자 값']
// obj = [filter, map, console.log]
// Object.keys(obj) 값 리턴  --> [0,1,2]




6. // each 과정

['인자 값']
// list = [filter, map, console.log]
// iter = function(val) {memo = iter(memo, val);});
// keyss = [0,1,2]

iter(list[keyss[i]], keyss[i]);

// list[keyss[0]] = filter ---> function(b) {return fn(b,a);}
// keyss[0] = 0

// list[keyss[1]] = map ---> function(b) {return fn(b,a);}
// keyss[1] = 1

// list[keyss[2]] = console.log ---> console.log
// keyss[2] = 2

iter(list[keyss[i]], keyss[i]);  // i 값을 순서대로 넣으면 밑에 처럼 된다.

iter(function(b){return fn(b,a), 0}); //호출
iter(function(b){return fn(b,a), 1}); //호출
iter(console.log, 2); //호출


7. // reduce 과정

['인자 값']
// list = [filter, map, console.log]
// iter = function(users,fn) {return fn(users)}
// memo = users


each(list, function(val) {memo = iter(memo, val);});

each([filter, map, console.log], users = iter(users,function(b){return fn(b,a);}))

// memo = users
// val = list[keyss[0]] ---> function(b) {return fn(b,a);}


iter(users, function(b){return fn(b,a);}) //호출



8. //pipe 과정

['인자 값']
// fns = [filter, map, console.log]
// arg = users


reduce(fns, function(arg, fn){return fn(arg);}, arg);

// fns = [filter, map, console.log]
// arg = users

// function(arg,fn){return fn(arg);}
// ----> function(users,function(b){return fn(b,a)}){return fn(users, function(user) {return user.age >=30;})}
fn(users, function(user) {return user.age <=30;})

filter(users, function(user){return user.age <=30;})

var filter = curryr(function(list, predi) {
    var new_list = [];
    each(list, function(val) {
        if (predi(val)) new_list.push(val);
    });
    return new_list;
});

new_list = [
        {id : 1, name : 'JM', age : 29},
        {id : 2, name : 'MJ', age : 28},
        {id : 3, name : 'SB', age : 25},
        {id : 4, name : 'YH', age : 27},
        {id : 5, name : 'SH', age : 26}]




9. // reduce 과정

['인자 값']
// list = [filter, map, console.log]
// iter = function(users,fn) {return fn(users)}
// memo = users


each(list, function(val) {memo = iter(memo, val);});

each([filter, map, console.log], users = iter(users,function(b){return fn(b,a);}))

// memo = users
// val = list[keyss[0]] ---> function(b) {return fn(b,a);}


iter(users, function(b){return fn(b,a);}) //호출

memo = [
    {id : 1, name : 'JM', age : 29},
    {id : 2, name : 'MJ', age : 28},
    {id : 3, name : 'SB', age : 25},
    {id : 4, name : 'YH', age : 27},
    {id : 5, name : 'SH', age : 26}]

map 과정


// map 실행 과정
map(get('name'))

// 6. each 과정 까지는 filter 과정과 동일




7. // reduce 과정

['인자 값']
// list = [filter, map, console.log]
// iter = function(users,fn) {return fn(users)}
// memo = users



each(list, function(val){memo = iter(memo, val)});

memo = [ //filter 거쳐서 나온 리스트
    {id : 1, name : 'JM', age : 29},
    {id : 2, name : 'MJ', age : 28},
    {id : 3, name : 'SB', age : 25},
    {id : 4, name : 'YH', age : 27},
    {id : 5, name : 'SH', age : 26}]

// val = list[keyss[1]]

each(list, memo = iter(memo, function(b){return fn(b,function(b){return fn(b,a)})}))


iter(memo, function(b){return fn(b,function(b){return fn(b,a)})}) // 호출



8. //pipe 과정

['인자 값']
// fns = [filter, map, console.log]
// function(arg, fn){return fn(arg)}
---> function(memo){return fn(memo,function(b){return fn(b,a)})}

map(memo,function(b){return fn(b,a)})


var map = curryr(function(list, mapper) {
    var new_list = [];
    each(list, function(val, key) {
        new_list.push(mapper(val, key));
    });
    return new_list;
});

list = [ //filter 거쳐서 나온 리스트
    {id : 1, name : 'JM', age : 29},
    {id : 2, name : 'MJ', age : 28},
    {id : 3, name : 'SB', age : 25},
    {id : 4, name : 'YH', age : 27},
    {id : 5, name : 'SH', age : 26}]

// mapper ---> function(b){return fn(b,a)}

function(list[keyss[i]], keys[i]) {new_list.push(mapper(list[keyss[i]], keys[i]))}

// mapper ---> function(b){return fn(b,a)}
    new_list.push(mapper({id : 1, name : 'JM', age : 29}, 0))
    new_list.push(mapper({id : 2, name : 'MJ', age : 28}, 1))
    new_list.push(mapper({id : 3, name : 'SB', age : 25}, 2))
    new_list.push(mapper({id : 4, name : 'YH', age : 27}, 3))
    new_list.push(mapper({id : 5, name : 'SH', age : 26}, 4))

// mapper ---> function(b){return fn(b,a)}
// b = {id : 1, name : 'JM', age : 29}    //자바스크립트는 인자를 1개 받는 함수에 2개 주면 앞에 1개 밖에 안받음

// mapper ---> function(b){return fn(b,a)}
function({id : 1, name : 'JM', age : 29}) {return fn({id : 1, name : 'JM', age : 29},a)}

    get({id : 1, name : 'JM', age : 29},'name')
    get({id : 2, name : 'MJ', age : 28},'name')
    get({id : 3, name : 'SB', age : 25},'name')
    get({id : 4, name : 'YH', age : 27},'name')
    get({id : 5, name : 'SH', age : 26},'name')

    new_list.push('JM')
    new_list.push('MJ')
    new_list.push('SB')
    new_list.push('YH')
    new_list.push('SH')


new_list = ['JM',"MJ",'SB','YH','SH']

console.log() 과정


// console.log 실행 과정
console.log

// 6. each 과정 까지는 filter,map 과정과 동일

var reduce = function(list, iter, memo) {
    if(arguments.length == 2) {
        memo = list[0];
        list = rest(list);
    }
    each(list, function(val) {             
        memo = iter(memo, val);
    });                                     
    return memo;                            
}

memo = ['JM',"MJ",'SB','YH','SH']

val = console.log

console.log(memo) 실행
memo = console.log(memo)
profile
안녕하세요.

0개의 댓글