Function composition: Difference between revisions

m
(→‎{{header|Diego}}: added Diego entry)
m (→‎JavaScript ES6 - Multiple composition: Updated primitive, tidied.)
Line 1,657:
====Multiple composition====
<lang JavaScript>(() => {
'"use strict'";
 
// -------------- MULTIPLE COMPOSITION ---------------
// compose :: [(a -> a)] -> (a -> a)
 
// compose (<<<) :: [(ab -> ac)] -> (a -> ab) -> a -> c
const compose = (...fs) =>
// A function defined by the right-to-left
x => fs.reduceRight(
// composition of all (a,the f)functions =>in f(a),fs.
xfs.reduce(
(f, g) => x => f(g(x)),
x => fs.reduceRight(x
);
 
// ---------------------- TEST -----------------------
// Test a composition of 3 functions (right to left)
const
sqrt = Math.sqrt,
Line 1,673 ⟶ 1,677:
 
return compose(half, succ, sqrt)(5);
 
// --> 1.618033988749895
})();</lang>
9,655

edits