Skip to content

Instantly share code, notes, and snippets.

@MikeMKH
Last active March 17, 2017 11:49
Show Gist options
  • Select an option

  • Save MikeMKH/630515b557345a134b538f0a1c5d1a76 to your computer and use it in GitHub Desktop.

Select an option

Save MikeMKH/630515b557345a134b538f0a1c5d1a76 to your computer and use it in GitHub Desktop.
Basic example of folktale core.arity with mocha, chai, and jsverify
const jsc = require('jsverify');
const {assert} = require('chai');
const pass = () => assert.isOk(true);
const fail = () => assert.fail();
const {nullary, unary, binary, ternary} = require('core.arity');
const {curry} = require('core.lambda');
describe('arity', () => {
describe('curried', () => {
const add = (x, y) => x + y;
const curried = curry(2, add);
jsc.property('curried add is same as non-curried', jsc.number, jsc.number,
(x, y) => curried(x)(y) === add(x, y)
),
jsc.property('curried add can curry or not curry', jsc.number, jsc.number,
(x, y) => curried(x)(y) === curried(x, y)
),
it('curried functions can fail when given too many arguments', () => {
try {
curried(1, 2, 3);
fail();
} catch(e) {
pass();
}
}),
jsc.property('binary can handle too many arguments', jsc.number, jsc.number, jsc.number,
(x, y, z) => binary(curried)(x, y, z) === curried(x, y)
);
}),
describe('nullary', () => {
const mkK = x => () => x;
jsc.property('make K returns value given', jsc.number,
x => mkK(x)() === x
),
jsc.property('nullary restricts it to a constant function', jsc.string, jsc.bool,
(s, b) => nullary(mkK(s))(b) === s
);
}),
describe('unary', () => {
const I = x => x;
jsc.property('I returns value given', jsc.datetime,
x => I(x) === x
),
jsc.property('unary restricts it to an unary function', jsc.string, jsc.bool,
(s, b) => unary(I)(s, b) === s
);
}),
describe('binary', () => {
const multiply = (x, y) => x * y;
jsc.property('multiply returns x * y', jsc.number, jsc.number,
(x, y) => multiply(x, y) === x * y
),
jsc.property('binary restricts it to a binary function', jsc.number, jsc.number, jsc.string,
(x, y, s) => binary(multiply)(x, y, s) === y * x
);
}),
describe('ternary', () => {
const tri = (pred, t, f) => pred ? t : f;
jsc.property('tri returns t if predicate is true else f', jsc.bool, jsc.nat, jsc.nat,
(pred, t, f) => tri(pred, t, f) === (pred ? t : f)
),
jsc.property('ternary restricts it to a ternary function', jsc.bool, jsc.number, jsc.string, jsc.datetime,
(pred, n, s, d) => ternary(tri)(pred, n, s) !== d
);
});
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment