Skip to content
This repository has been archived by the owner on Jul 25, 2018. It is now read-only.

Commit

Permalink
Merge pull request #10 from fantasyland/ord-experimental
Browse files Browse the repository at this point in the history
adds ord
  • Loading branch information
SimonRichardson committed Mar 2, 2016
2 parents 974f6a2 + 5a0c706 commit 98b6b50
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 38 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
| Endo | function identity | function composition |
| Multiplicative | 1 | * |
| Unit | {} | {} |
| Min | Number.MAX_VALUE | < |
| Max | Number.MIN_VALUE | > |
| Min(Ord) | Ord.min | Ord.compare |
| Max(Ord) | Ord.max | Ord.compare |


## Testing
Expand Down
10 changes: 8 additions & 2 deletions fantasy-monoids.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const Dual = require('./src/dual');
const Endo = require('./src/endo');
const Multiplicative = require('./src/multiplicative');
const Unit = require('./src/unit');
const Min = require('./src/min');
const Max = require('./src/max');
const Ord = require('./src/ord');
const Min = require('./src/min');
const Max = require('./src/max');
const concat = require('./src/concat');
const mconcat = require('./src/mconcat');

module.exports = { Additive
, Conjunction
Expand All @@ -19,4 +22,7 @@ module.exports = { Additive
, Unit
, Min
, Max
, Ord
, concat
, mconcat
};
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
"fantasy-combinators": "git+https://github.com/fantasyland/fantasy-combinators.git"
},
"devDependencies": {
"nodeunit": "0.9.x",
"fantasy-check": "git+https://github.com/fantasyland/fantasy-check.git",
"fantasy-identities": "git+https://github.com/fantasyland/fantasy-identities.git"
"fantasy-identities": "git+https://github.com/fantasyland/fantasy-identities.git",
"mocha": "^2.4.5",
"nodeunit": "0.9.x"
},
"scripts": {
"test": "node --harmony_destructuring node_modules/.bin/nodeunit test/*.js"
"test": "node --harmony_destructuring node_modules/.bin/nodeunit test/*.js"
},
"files": ["fantasy-monoids.js", "src/*.js"],
"files": [
"fantasy-monoids.js",
"src/*.js"
],
"main": "fantasy-monoids.js",
"version": "0.0.1"
}
3 changes: 3 additions & 0 deletions src/concat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const concat = (x, y) => x.concat(y);

module.exports = concat;
21 changes: 13 additions & 8 deletions src/max.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

const {tagged} = require('daggy');
const {empty, of, concat, equals} = require('fantasy-land');
const {compare, min, GT} = require('./ord');

const Max = tagged('x');
const Max = Ord => {
const Max = tagged('x');

Max[of] = x => Max(x);
Max[empty] = () => Max(Number.MIN_VALUE);
Max[of] = x => Max(Ord[of](x));
Max[empty] = () => Max(Ord[min]());

Max.prototype[equals] = function(y) {
return this.x === y.x;
};
Max.prototype[concat] = function(y) {
return Max(this.x > y.x ? this.x : y.x);
Max.prototype[equals] = function(y) {
return this.x[equals](y.x);
};
Max.prototype[concat] = function(y) {
return Max(this.x[compare](y.x) === GT ? this.x: y.x);
};

return Max;
};

module.exports = Max;
5 changes: 5 additions & 0 deletions src/mconcat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const concat = require('./concat');

const mconcat = (xs, empty) => xs.length ? xs.reduce(concat) : empty();

module.exports = mconcat;
21 changes: 13 additions & 8 deletions src/min.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@

const {tagged} = require('daggy');
const {empty, of, concat, equals} = require('fantasy-land');
const {compare, max, LT} = require('./ord');

const Min = tagged('x');
const Min = Ord => {
const Min = tagged('x');

Min[of] = x => Min(x);
Min[empty] = () => Min(Number.MAX_VALUE);
Min[of] = x => Min(Ord[of](x));
Min[empty] = () => Min(Ord[max]());

Min.prototype[equals] = function(y) {
return this.x === y.x;
};
Min.prototype[concat] = function(y) {
return Min(this.x < y.x ? this.x : y.x);
Min.prototype[equals] = function(y) {
return this.x[equals](y.x);
};
Min.prototype[concat] = function(y) {
return Min(this.x[compare](y.x) === LT ? this.x : y.x);
};

return Min;
};

module.exports = Min;
38 changes: 38 additions & 0 deletions src/ord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const {tagged} = require('daggy');
const {empty, of, concat, equals} = require('fantasy-land');

const Ord = M => {

const _Ord = tagged('x');

_Ord[of] = _Ord;
_Ord[Ord.min] = M[Ord.min];
_Ord[Ord.max] = M[Ord.max];

_Ord.prototype.compare = function(y) {
return M.compare(this.x, y.x);
};
_Ord.prototype[equals] = function(y) {
return M.compare(this.x, y.x) === Ord.EQ;
};
_Ord.prototype.lt = function(y) {
return M.compare(this.x, y.x) === Ord.LT;
};
_Ord.prototype.gt = function(y) {
return M.compare(this.x, y.x) === Ord.GT;
};

return _Ord;
};

Ord.EQ = 0;
Ord.GT = 1;
Ord.LT = -1;

Ord.compare = 'compare';
Ord.min = 'min';
Ord.max = 'max';

module.exports = Ord;
44 changes: 37 additions & 7 deletions test/max.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,54 @@ const m = require('fantasy-land/laws/monoid');
const s = require('fantasy-land/laws/semigroup');
const = require('fantasy-land/laws/setoid');

const {Max} = require('../fantasy-monoids');
const {Max, Ord, mconcat} = require('../fantasy-monoids');
const Ordʹ = Ord({ min: () => Ordʹ(Number.MIN_NUMBER)
, max: () => Ordʹ(Number.MAX_NUMBER)
, compare: (x, y) => x === y ? Ord.EQ : x < y ? Ord.LT : Ord.GT
});
const Maxʹ = Max(Ordʹ);

exports.monoid = {

'rightIdentity': λ.law(m.rightIdentity)(Max),
'leftIdentity': λ.law(m.leftIdentity)(Max)
'rightIdentity': λ.law(m.rightIdentity)(Maxʹ),
'leftIdentity': λ.law(m.leftIdentity)(Maxʹ)
};


exports.semigroup = {

'associativity': λ.law(s.associativity)(Max)
'associativity': λ.law(s.associativity)(Maxʹ.of)
};


exports.setoid = {

'reflexivity': λ.law(.reflexivity)(Max),
'symmetry': λ.law(.symmetry)(Max),
'transitivity': λ.law(.transitivity)(Max)
'reflexivity': λ.law(.reflexivity)(Maxʹ.of),
'symmetry': λ.law(.symmetry)(Maxʹ.of),
'transitivity': λ.law(.transitivity)(Maxʹ.of)
};

exports.basicUsage = test => {
const expected = { x: { x: 9 } };

test.deepEqual(
Maxʹ(Ordʹ(3))
.concat(Maxʹ(Ordʹ(6)))
.concat(Maxʹ(Ordʹ(8)))
.concat(Maxʹ(Ordʹ(9)))
.concat(Maxʹ(Ordʹ(1)))
, expected
)
test.deepEqual(
mconcat([ Maxʹ(Ordʹ(3))
, Maxʹ(Ordʹ(6))
, Maxʹ(Ordʹ(8))
, Maxʹ(Ordʹ(9))
, Maxʹ(Ordʹ(1))
], Maxʹ.empty()
)
, expected);


test.done();
};
44 changes: 37 additions & 7 deletions test/min.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,54 @@ const m = require('fantasy-land/laws/monoid');
const s = require('fantasy-land/laws/semigroup');
const = require('fantasy-land/laws/setoid');

const {Min} = require('../fantasy-monoids');
const {Min, Ord, mconcat} = require('../fantasy-monoids');
const Ordʹ = Ord({ min: () => Ordʹ(Number.MIN_NUMBER)
, max: () => Ordʹ(Number.MAX_NUMBER)
, compare: (x, y) => x === y ? Ord.EQ : x < y ? Ord.LT : Ord.GT
});
const Minʹ = Min(Ordʹ);

exports.monoid = {

'rightIdentity': λ.law(m.rightIdentity)(Min),
'leftIdentity': λ.law(m.leftIdentity)(Min)
'rightIdentity': λ.law(m.rightIdentity)(Minʹ),
'leftIdentity': λ.law(m.leftIdentity)(Minʹ)
};


exports.semigroup = {

'associativity': λ.law(s.associativity)(Min)
'associativity': λ.law(s.associativity)(Minʹ.of)
};


exports.setoid = {

'reflexivity': λ.law(.reflexivity)(Min),
'symmetry': λ.law(.symmetry)(Min),
'transitivity': λ.law(.transitivity)(Min)
'reflexivity': λ.law(.reflexivity)(Minʹ.of),
'symmetry': λ.law(.symmetry)(Minʹ.of),
'transitivity': λ.law(.transitivity)(Minʹ.of)
};

exports.basicUsage = test => {
const expected = { x: { x: 1 } };

test.deepEqual(
Minʹ(Ordʹ(3))
.concat(Minʹ(Ordʹ(6)))
.concat(Minʹ(Ordʹ(8)))
.concat(Minʹ(Ordʹ(9)))
.concat(Minʹ(Ordʹ(1)))
, expected
)
test.deepEqual(
mconcat([ Minʹ(Ordʹ(3))
, Minʹ(Ordʹ(6))
, Minʹ(Ordʹ(8))
, Minʹ(Ordʹ(9))
, Minʹ(Ordʹ(1))
], Minʹ.empty()
)
, expected);


test.done();
};
21 changes: 21 additions & 0 deletions test/ord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

const {adapters: {nodeunit: λ}} = require('fantasy-check');
const {identity} = require('fantasy-combinators');

const = require('fantasy-land/laws/setoid');

const {Ord} = require('../fantasy-monoids');

const Ordʹ = Ord({ min: () => undefined
, max: () => undefined
, compare: (x, y) => x === y ? Ord.EQ: x < y ? Ord.LT: Ord.GT
});


exports.setoid = {

'reflexivity': λ.law(.reflexivity)(Ordʹ.of),
'symmetry': λ.law(.symmetry)(Ordʹ.of),
'transitivity': λ.law(.transitivity)(Ordʹ.of)
};

0 comments on commit 98b6b50

Please sign in to comment.