Create a Pipe Method (Medium)
Good afternoon! Here's our prompt for today.
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:
1const times = (y) => (x) => x * y
2const plus = (y) => (x) => x + y
3const subtract = (y) => (x) => x - y
4const divide = (y) => (x) => x / y
The pipe()
would be used to generate new functions:
1pipe([
2 times(2),
3 times(3)
4])
5// x * 2 * 3
6
7pipe([
8 times(2),
9 plus(3),
10 times(4)
11])
12// (x * 2 + 3) * 4
13
14pipe([
15 times(2),
16 subtract(3),
17 divide(4)
18])
19// (x * 2 - 3) / 4
Can you implement a pipe()
function that will work as the examples stated above?

xxxxxxxxxx
pipe([
times(2),
subtract(3),
divide(4)
])