diff --git a/examples/ord/Ord.test.ts b/examples/ord/Ord.test.ts new file mode 100644 index 0000000..b4824c9 --- /dev/null +++ b/examples/ord/Ord.test.ts @@ -0,0 +1,87 @@ +import * as TL from './Ord' +import * as fc from 'fast-check' + +const trafficLightArb: fc.Arbitrary = fc.oneof( + fc.constant(TL.red), + fc.constant(TL.yellow), + fc.constant(TL.yellowFlashing), + fc.constant(TL.green) +) + +describe('properties', () => { + // the "zero" is always the lowest so does nothing + it('a + zero == a', () => { + fc.assert( + fc.property( + trafficLightArb, + (tl) => + TL.trafficLightJoin.join(tl, TL.trafficLightJoin.zero) === + tl + ) + ) + }) + + it('a + green == green', () => { + fc.assert( + fc.property( + trafficLightArb, + (tl) => TL.trafficLightJoin.join(tl, TL.green) === TL.green + ) + ) + }) + + it('a + b == b + a', () => { + fc.assert( + fc.property( + trafficLightArb, + trafficLightArb, + (tlA, tlB) => + TL.trafficLightJoin.join(tlA, tlB) === + TL.trafficLightJoin.join(tlB, tlA) + ) + ) + }) + + it('a + (b + c) == (a + b) + c', () => { + fc.assert( + fc.property( + trafficLightArb, + trafficLightArb, + trafficLightArb, + (tlA, tlB, tlC) => + TL.trafficLightJoin.join( + tlA, + TL.trafficLightJoin.join(tlB, tlC) + ) === + TL.trafficLightJoin.join( + TL.trafficLightJoin.join(tlA, tlB), + tlC + ) + ) + ) + }) + + it('a + a == a', () => { + fc.assert( + fc.property( + trafficLightArb, + (tlA) => TL.trafficLightJoin.join(tlA, tlA) === tlA + ) + ) + }) + + it('a + b + b == a + b', () => { + fc.assert( + fc.property( + trafficLightArb, + trafficLightArb, + (tlA, tlB) => + TL.trafficLightJoin.join(tlA, tlB) === + TL.trafficLightJoin.join( + TL.trafficLightJoin.join(tlA, tlB), + tlB + ) + ) + ) + }) +}) diff --git a/examples/ord/Ord.ts b/examples/ord/Ord.ts new file mode 100644 index 0000000..3845510 --- /dev/null +++ b/examples/ord/Ord.ts @@ -0,0 +1,33 @@ +import * as BJS from 'fp-ts/BoundedJoinSemilattice' +import * as Ord from 'fp-ts/Ord' +import { Ord as numOrd } from 'fp-ts/number' + +const mk = (a: A) => a + +export const red = mk('red') +export const yellow = mk('yellow') +export const yellowFlashing = mk('yellow-flashing') +export const green = mk('green') + +export type TrafficLight = + | 'red' + | 'yellow' + | 'yellow-flashing' + | 'green' + +const tlNums: Record = { + red: 0, + yellow: 1, + 'yellow-flashing': 2, + green: 3, +} + +const trafficLightOrd: Ord.Ord = { + equals: (a, b) => numOrd.equals(tlNums[a], tlNums[b]), + compare: (a, b) => numOrd.compare(tlNums[a], tlNums[b]), +} + +export const trafficLightJoin: BJS.BoundedJoinSemilattice = { + join: Ord.max(trafficLightOrd), + zero: 'red', +}