Curry, compose and pipe
Small functions joined into bigger ones. pipe runs left to right.
JavaScript · Curry, compose and pipeAdvanced
const curry = (fn) =>
function curried(...args) {
return args.length >= fn.length ? fn(...args) : (...more) => curried(...args, ...more);
};
const pipe = (...fns) => (x) => fns.reduce((v, f) => f(v), x);
const compose = (...fns) => (x) => fns.reduceRight((v, f) => f(v), x);
const add = curry((a, b) => a + b);
const addTax = add(17); // partially applied
const slugify = pipe(
(s) => s.trim().toLowerCase(),
(s) => s.replace(/[^a-z0-9]+/g, '-'),
(s) => s.replace(/^-|-$/g, '')
);functionalcurrycomposepipe