RosettaCodeData/Task/Function-composition/JavaScript/function-composition-4.js

21 lines
408 B
JavaScript
Raw Normal View History

2016-12-05 22:15:40 +01:00
(() => {
'use strict';
// compose :: [(a -> a)] -> (a -> a)
2019-09-12 10:33:56 -07:00
const compose = (...fs) =>
x => fs.reduceRight(
(a, f) => f(a),
x
);
2016-12-05 22:15:40 +01:00
2019-09-12 10:33:56 -07:00
// Test a composition of 3 functions (right to left)
const
sqrt = Math.sqrt,
2016-12-05 22:15:40 +01:00
succ = x => x + 1,
half = x => x / 2;
2019-09-12 10:33:56 -07:00
return compose(half, succ, sqrt)(5);
2016-12-05 22:15:40 +01:00
// --> 1.618033988749895
})();