5 kyu

Compose functions (T Combinator)

2,247 of 2,293kylehill

Description:

Let's make a function called compose that accepts a value as a parameter, as well as any number of functions as additional parameters.

The function will return the value that results from the first parameter being used as a parameter for all of the accepted function parameters in turn.

compose(n, f1, f2, f3..., fn) // === fn(...(f3(f2(f1(n)))))

So:

var doubleTheValue = function(val) { return val * 2; }
var addOneToTheValue = function(val) { return val + 1; }

compose(5, doubleTheValue) // should === 10
compose(5, doubleTheValue, addOneToTheValue) // should === 11

If only a single parameter is passed in, return that parameter.

compose(n) = n
Functional Programming
Fundamentals

Stats:

CreatedAug 1, 2013
PublishedAug 1, 2013
Warriors Trained3727
Total Skips654
Total Code Submissions12070
Total Times Completed2293
JavaScript Completions2247
Total Stars46
% of votes with a positive feedback rating91% of 246
Total "Very Satisfied" Votes206
Total "Somewhat Satisfied" Votes37
Total "Not Satisfied" Votes3
Ad
Contributors
  • kylehill Avatar
  • flaco Avatar
Ad