EasyJavaScriptTypeScript

Implement pipe()

FunctionsDesign

Implement solve(fnExprs, initial) where fnExprs is an array of arrow
function expression strings and initial is the starting value.

Apply each function left-to-right, passing the result of each to the next
(like Unix pipes or Array.prototype.reduce).

solve(['x => x + 1', 'x => x * 2', 'x => x - 3'], 5)
(5 + 1) * 2 - 39

Rules

  • Compile each expression with new Function('return (' + expr + ';)()')).
  • If fnExprs is empty, return initial unchanged.

Sample tests

Test #1Three transforms
Input: [["x => x + 1","x => x * 2","x => x - 3"],5]
Output: 9
Test #2Empty pipe returns initial
Input: [[],42]
Output: 42
Test #3Single transform
Input: [["x => x * x"],4]
Output: 16
Test #4Add then halve
Input: [["x => x + 10","x => x / 2"],6]
Output: 8
Test #5Negate
Input: [["x => -x"],7]
Output: -7