Last active
August 29, 2015 14:26
Fold And in ECMAScript2015 with babel, mocha, expect.js, and lodash
This file contains 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
let _ = require("lodash"); | |
exports.foldAnd = (...facts) => | |
_.foldl(facts, (m, x) => m && x, true); |
This file contains 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
let sut = require("../src/foldAnd"), | |
expect = require("expect.js"), | |
_ = require("lodash"); | |
describe("foldAnd", () => { | |
it("Given all true it must return true", () => { | |
expect(sut.foldAnd(true)).to.be.ok(); | |
expect(sut.foldAnd(true, true)).to.be.ok(); | |
expect(sut.foldAnd(true, true, true)).to.be.ok(); | |
}), | |
it("Given all false it must return false", () => { | |
expect(sut.foldAnd(false)).not.to.be.ok(); | |
expect(sut.foldAnd(false, false)).not.to.be.ok(); | |
expect(sut.foldAnd(false, false, false)).not.to.be.ok(); | |
}), | |
it("Given some true and false it must return the same result as every", () => { | |
expect(sut.foldAnd(true, false)).to.equal(_.every([true, false])); | |
expect(sut.foldAnd(false, true)).to.equal(_.every([false, true])); | |
expect(sut.foldAnd(false, true, false)).to.equal(_.every([false, true, false])); | |
expect(sut.foldAnd(false, false, true)).to.equal(_.every([false, false, true])); | |
}) | |
}); |
See also my blog post which goes with this gist http://comp-phil.blogspot.com/2015/08/approaching-fold.html
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on http://www.cs.nott.ac.uk/~gmh/fold.pdf