Jump to the editorJAVASCRIPT
JAVASCRIPT JAVASCRIPT JAVASCRIPT
6.9. Negate a predicate; compose and pipe functions
Easy
Topics
This question is in
6. Functions and closuresSmall functions are easy to test, and real work is often a chain of them: trim the input, lower-case it, turn it into a slug. Writing slug(lower(trim(text))) everywhere is noisy, and a filter that needs "everything that is not archived" should not need a new function written by hand. Three tiny tools combine functions into new ones.
Write negate, compose and pipe in compose.js.
Signature
function negate(predicate) // returns a function
function compose(...fns) // returns a function
function pipe(...fns) // returns a functionnegate(predicate): the returned function callspredicateand returns the opposite, always as a boolean.pipe(f, g, h): the returned function doesh(g(f(...args))):fgets every argument, each next function gets the previous result.compose(f, g, h): the same, from right to left:f(g(h(...args))).- With no functions,
pipe()andcompose()return a function that returns its first argument.
Requirements
negate's function returnstrueexactly whenpredicatereturns a falsy value, and is always a boolean.negate's function passes itsthisand every argument topredicate.piperuns its functions left to right; the first gets every argument, the others get the previous result only.composeruns its functions right to left; the last gets every argument, the others get the previous result only.- With no functions, the result returns its first argument; with one function, it behaves like that function.
- The combined function passes its
thisto every function it runs. - Each tool throws a
TypeErrorstraight away, before any call, when given something that is not a function. - A combined function can be called any number of times and gives the same results each time.
Examples
const isEven = (n) => n % 2 === 0;
[1, 2, 3, 4].filter(negate(isEven)); // [1, 3]
negate(() => "yes")(); // falseconst trim = (s) => s.trim();
const lower = (s) => s.toLowerCase();
const dashes = (s) => s.replace(/\s+/g, "-");
pipe(trim, lower, dashes)(" Hello World "); // "hello-world"
compose(dashes, lower, trim)(" Hello World "); // "hello-world"const add = (a, b) => a + b;
const double = (n) => n * 2;
pipe(add, double)(3, 4); // 14: add gets both arguments
compose(double, add)(3, 4); // 14
pipe()("same"); // "same"Environment
- Plain JavaScript in a browser page; the tests call the globals
negate,composeandpipefromcompose.js. - Out of scope: async functions (a promise is passed on like any other value) and stopping a chain early.
Hints
composeandpipediffer only in order. Can one be written with the other?- What should a chain of zero functions do with its input?
The explanation comes with the course
A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.
- Every line explained
- The mistakes each test catches
- Links to the lessons behind it
Back to the question
Loading the editor…