Skip to curriculum
Jump to the editor

6.9. Negate a predicate; compose and pipe functions

Easy
Topics

This question is in

6. Functions and closures
15–20 min

Small 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

JAVASCRIPT
function negate(predicate)  // returns a function
function compose(...fns)    // returns a function
function pipe(...fns)       // returns a function
  • negate(predicate): the returned function calls predicate and returns the opposite, always as a boolean.
  • pipe(f, g, h): the returned function does h(g(f(...args))): f gets 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() and compose() return a function that returns its first argument.

Requirements

  1. negate's function returns true exactly when predicate returns a falsy value, and is always a boolean.
  2. negate's function passes its this and every argument to predicate.
  3. pipe runs its functions left to right; the first gets every argument, the others get the previous result only.
  4. compose runs its functions right to left; the last gets every argument, the others get the previous result only.
  5. With no functions, the result returns its first argument; with one function, it behaves like that function.
  6. The combined function passes its this to every function it runs.
  7. Each tool throws a TypeError straight away, before any call, when given something that is not a function.
  8. A combined function can be called any number of times and gives the same results each time.

Examples

JAVASCRIPT
const isEven = (n) => n % 2 === 0;
[1, 2, 3, 4].filter(negate(isEven)); // [1, 3]
negate(() => "yes")();               // false
JAVASCRIPT
const 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"
JAVASCRIPT
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, compose and pipe from compose.js.
  • Out of scope: async functions (a promise is passed on like any other value) and stopping a chain early.

Hints

  1. compose and pipe differ only in order. Can one be written with the other?
  2. 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…