AlgoDaily

Create a Pipe Method

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Question

Composition is putting two or more different things together, and getting a combination of the inputs as a result. In terms of programming, composition is often used for combining multiple functions and getting a result at the end. Using composition leads to a cleaner and much more compact code.

If we want to call multiple functions, at different places in our application, we can write a function that will do this. This type of function is called a pipe, since it puts the functions together and returns a single output.

A pipe function would have the following functionality:

Suppose we have some simple functions like this:

const times = (y) =>  (x) => x * y
const plus = (y) => (x) => x + y
const subtract = (y) =>  (x) => x - y
const divide = (y) => (x) => x / y

The pipe() would be used to generate new functions:

pipe([
  times(2),
  times(3)
])  
// x * 2 * 3

pipe([
  times(2),
  plus(3),
  times(4)
]) 
// (x * 2 + 3) * 4

pipe([
  times(2),
  subtract(3),
  divide(4)
]) 
// (x * 2 - 3) / 4

Can you implement a pipe() function that will work as the examples stated above?

cover

Solution


Our solution for the pipe() function will be fairly simple. For every function call in the argument array funcs, it will return a new function.

This one will perform a reduce operation, which will call the sequence of functions and give them their arguments. Each argument of reduce() has to return the values that the next function requires.

From the perspective of the next function, the parameter that should be passed into it should come from the previous call of reduce(). In reduce(), result is the return value and func is the current value of the funcs array.

return funcs.reduce((result, func) => {
  return func(result);
}, arg);

Since the reduce() method also requires an initialValue, we will provide it with the current arg, because if not provided, the first element of an array will be used as initialValue and this will be a function and it will be omitted from execution. Then, the second element of an array will be used as the first func of the loop.

So, the solution would look like this:

function pipe(funcs) {
  return function (arg) {
    return funcs.reduce((result, func) => {
      return func(result);
    }, arg);
  }
}

So, in terms of some of the previous examples, the execution would go something like this:

pipe([
  times(2),
  subtract(3),
  divide(4)
])
  1. The reduce method gets times(2) as a parameter, and performs its action (multiplies by 2)
  2. Then the result returned gets passed to the subtract(3) function, and 3 is subtracted from the result
  3. Finally the operation divide(4) gets performed over the current result, and it gets divided by 4

One Pager Cheat Sheet

One Pager Cheat Sheet

  • Using composition, we can create a pipe function to call multiple functions and return a single output in a cleaner, more compact way.
  • We can simply reduce() an argument array of functions, each performing a different action according to their parameters and the result of the previous call.
Open interactive view →