본문 바로가기

개발/ES6

함수형 프로그래밍 - Reduce를 활용하여 Map, Filter 구현하기

let list = [1, 3, 4, 5, 6, 10];

function filter(list, predicate) {
    let new_list = [];
    for (let i = 0; i < list.length; i++) {
        if (predicate(list[i])) new_list.push(list[i]);
    }
    return new_list;
}

function predicate(n){
    return n > 5;
}

let filterResult = filter(list, predicate);
console.log(filterResult);

function myMap(list, func) {
    let result = [];
    for (let i = 0; i < list.length; i++) {
        result.push(func(list[i]));
    }
    return result;
}

function square(n) {
    return n * n;
}

let mapResult = myMap(list, square);
console.log(mapResult);

//////////////////////////////////////////////

/*
 * Reduce로 Map 구현
 */
function myReduceForMap(list, func, init) {
    let result = init;      // []

    for (let i = 0; i < list.length; i++) {
        result.push(func(list[i]));
    }
    return result;
}


function plus(acc, value) {
    return parseInt(acc + value);
}

let reduceResult = myReduceForMap(list, plus, []);
console.log(`reduceMapResult : ${mapResult}`);


//////////////////////////////////////////////////////////////

/*
 * Reduce로 Filter 구현
 */
function myReduceForFilter(list, func, init) {
    let result = init;          // []

    for (let i = 0; i < list.length; i++) {
        if(func(list[i])) {
            result.push(list[i]);
        }
    }
    return result;
}

function ageThreeUp(n) {
    return n >= 3;
}

let reduceFilterResult = myReduceForFilter(list, ageThreeUp, []);
console.log(`reduceFilterResult : ${reduceFilterResult}`);

//////////////////////////////////////////////////////////////

/*
 * Reduce로 Reduce 구현
 * ----> 초깃값 없을 때
 */
function myReduce(list, func, init) {
    let result = init;
    for (let i = 1; i < list.length; i++) {
        result = func(list[i-1], list[i]);
    }
    return result;
}

function multiply(acc, value) {
    console.log(`${acc} * ${value} = ${acc * value}`);
    acc = acc * value;
    return acc;
}

let ReduceMultiply = myReduce(list, multiply, list[0]);
console.log(ReduceMultiply);


//////////////////////////////////////////////////////////////

// 원조(?) reduce 메서드로 map 만들기

const originalReduceMakeMap = list.reduce((accumulator, currentValue, currentIndex) => , []);

console.log(typeof originalReduceMakeMap);
console.log(originalReduceMakeMap);​
let list = [1, 3, 4, 5, 6, 10];

위 list 배열을 가지고 map, filter, reduce, reduce를 이용한 map, reduce를 이용한 filter,

ES6의 reduce를 활용하여 map과 filter를 구현해보려고 한다.


 

Map
function myMap(list, func) {
    let result = [];
    for (let i = 0; i < list.length; i++) {
        result.push(func(list[i]));
    }
    return result;
}
function square(n) {
    return n * n;
}
let mapResult = myMap(list, square);
console.log(mapResult);

 


 

Filter
function filter(list, predicate) {
    let new_list = [];
    for (let i = 0; i < list.length; i++) {
        if (predicate(list[i])) new_list.push(list[i]);
    }
    return new_list;
}
function predicate(n){
    return n > 5;
}
let filterResult = filter(list, predicate);
console.log(filterResult);

 


 

Reduce For Map
function myReduceForMap(list, func, init) {
    let result = init;      // []

    for (let i = 0; i < list.length; i++) {
        result.push(func(list[i]));
    }
    return result;
}


function plus(acc, value) {
    return acc + value;
}

let reduceResult = myReduceForMap(list, plus, []);
console.log(`reduceMapResult : ${mapResult}`);

 

 

 

Reduce For Filter
/*
 * Reduce로 Filter 구현
 */
function myReduceForFilter(list, func, init) {
    let result = init;          // []

    for (let i = 0; i < list.length; i++) {
        if(func(list[i])) {
            result.push(list[i]);
        }
    }
    return result;
}

function ageThreeUp(n) {
    return n >= 3;
}

let reduceFilterResult = myReduceForFilter(list, ageThreeUp, []);
console.log(`reduceFilterResult : ${reduceFilterResult}`);

 

 

Reduce
/*
 * Reduce로 Reduce 구현
 * ----> 초깃값 없을 때
 */
function myReduce(list, func, init) {
    let result = init;
    for (let i = 1; i < list.length; i++) {
        result = func(list[i-1], list[i]);
    }
}

function multiply(acc, value) {
    console.log(`${acc} * ${value} = ${acc * value}`);
    acc = acc * value;
    return acc;
}

let ReduceMultiply = myReduce(list, multiply, list[0]);
console.log(ReduceMultiply);