composePipe

Written on August 6, 2019

문제

Write Compose and Pipe functions.

Step 1: Implement the function Compose:

Compose should return a function that is the composition of a list of functions of arbitrary length. Each function is called on the return value of the function that follows. You can view compose as moving right to left through its arguments.

Compose Example:

var greet = function(name) {
  return "hi: " + name;
};
var exclaim = function(statement) {
  return statement.toUpperCase() + "!";
};
var welcome = compose(
  greet,
  exclaim
);
welcome("phillip"); // 'hi: PHILLIP!'

Step 2: Implement the function Pipe:

Pipe composes a series of functions and returns the resulting function. Each function is called on the return value of the preceding function. You can view pipe as moving left to right through its arguments.

Pipe Example:


var add2 = function(number){ return number + 2; }
var multiplyBy3 = function(number){ return number \* 3; }
pipe(add2, multiplyBy3)(5) // 21
pipe(add2, multiplyBy3, multiplyBy3)(5) // 63

풀이

var compose = function() {
  let args = Array.from(arguments).reverse();
  return value => {
    return args.reduce((accu, curr) => curr(accu), value);
  };
};

var pipe = function() {
  let args = Array.from(arguments);
  return value => {
    return args.reduce((accu, curr) => curr(accu), value);
  };
};

👩🏻‍💻 배우는 것을 즐기는 프론트엔드 개발자 입니다
부족한 블로그에 방문해 주셔서 감사합니다 🙇🏻‍♀️

in the process of becoming the best version of myself