Last active
September 8, 2020 08:55
-
-
Save perjo927/f56e68cb06908633ad5df62ddb595f64 to your computer and use it in GitHub Desktop.
Proof for function composition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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