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

adds ord #10

Merged
merged 1 commit into from
Mar 2, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why prototype[equals] instead of just prototype.equals ? just looks weird to me ;)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is the new fantasyland naming scheme, you can read all of this
if you want fantasyland/fantasy-land#92

On Sun, 21 Feb 2016, 09:51 Tobias Pflug notifications@github.com wrote:

In src/ord.js
#10 (comment)
:

+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) {

Why prototype[equals] instead of just prototype.equals ? just looks weird
to me ;)


Reply to this email directly or view it on GitHub
https://github.com/fantasyland/fantasy-monoids/pull/10/files#r53564307.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SimonRichardson wow that is an interesting read. Thanks for the link!

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 sʹ = 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(sʹ.reflexivity)(Max),
'symmetry': λ.law(sʹ.symmetry)(Max),
'transitivity': λ.law(sʹ.transitivity)(Max)
'reflexivity': λ.law(sʹ.reflexivity)(Maxʹ.of),
'symmetry': λ.law(sʹ.symmetry)(Maxʹ.of),
'transitivity': λ.law(sʹ.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 sʹ = 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(sʹ.reflexivity)(Min),
'symmetry': λ.law(sʹ.symmetry)(Min),
'transitivity': λ.law(sʹ.transitivity)(Min)
'reflexivity': λ.law(sʹ.reflexivity)(Minʹ.of),
'symmetry': λ.law(sʹ.symmetry)(Minʹ.of),
'transitivity': λ.law(sʹ.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 sʹ = 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(sʹ.reflexivity)(Ordʹ.of),
'symmetry': λ.law(sʹ.symmetry)(Ordʹ.of),
'transitivity': λ.law(sʹ.transitivity)(Ordʹ.of)
};