Skip to content

Instantly share code, notes, and snippets.

@perjo927
Last active September 8, 2020 08:55
Show Gist options
  • Save perjo927/f56e68cb06908633ad5df62ddb595f64 to your computer and use it in GitHub Desktop.
Save perjo927/f56e68cb06908633ad5df62ddb595f64 to your computer and use it in GitHub Desktop.
Proof for function composition
import assert from "assert";
const compose = (...functions) => (initialArg) =>
functions.reduceRight(
(accumulatedValue, func) => func(accumulatedValue),
initialArg
);
describe("compose", () => {
it("returns a composed function h, so that h(x) is equal to f(g(x))", () => {
const x = 300;
const f = (x) => x + 1000;
const g = (x) => x + 37;
const h = compose(f, g);
const outputfromG = g(x);
const outputFromF = f(outputfromG);
const outputFromH = h(x);
const expectedOutput = 1337;
assert.equal(outputFromF, expectedOutput);
assert.equal(outputFromH, expectedOutput);
assert.equal(outputFromH, outputFromF);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment