MediumPro challengePythonJavaScriptTypeScript

Pipe & Compose

FunctionsPatternsClosures

Implement pipe(...fns) and compose(...fns) that chain unary functions
together.

  • pipe(f, g, h)(x) applies left-to-right: h(g(f(x)))
  • compose(f, g, h)(x) applies right-to-left: f(g(h(x)))

solve(value, fns, mode) returns the result of piping or composing a list
of math functions over value. mode is 'pipe' or 'compose'.

Sample tests

Test #1Empty pipeline → identity
Input: [3,[],"pipe"]
Output: 3
Test #2pipe: 2→double→4, inc→5, square→25
Input: [2,["double","inc","square"],"pipe"]
Output: 25
Test #3Three increments
Input: [5,["inc","inc","inc"],"pipe"]
Output: 8
Test #4compose: square(2)=4, inc→5, double→10
Input: [2,["double","inc","square"],"compose"]
Output: 10
Test #5compose(square,double)(3): reduceRight → double(3)=6 first, then square(6)=36
Input: [3,["square","double"],"compose"]
Output: 36