Skip to content

Commit

Permalink
ord: add S.lt, S.lte, S.gt, and S.gte
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Dec 6, 2016
1 parent f32201e commit d6b3822
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 0 deletions.
92 changes: 92 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3060,6 +3060,98 @@
}
S.mean = def('mean', {f: [Foldable]}, [f, $Maybe($.FiniteNumber)], mean);

//# lt :: Ord a => a -> a -> Boolean
//.
//. Returns `true` if its first argument is smaller than its second
//. argument; `false` otherwise.
//.
//. Strings are compared lexicographically. Specifically, the Unicode
//. code point value of each character in the first string is compared
//. to the value of the corresponding character in the second string.
//.
//. See also [`lte`](#lte), [`gt`](#gt), and [`gte`](#gte).
//.
//. ```javascript
//. > S.lt(10, 2)
//. false
//.
//. > S.lt('10', '2')
//. true
//. ```
function lt(x, y) {
return x < y;
}
S.lt = def('lt', {a: [Ord]}, [a, a, $.Boolean], lt);

//# lte :: Ord a => a -> a -> Boolean
//.
//. Returns `true` if its first argument is smaller than or equal to
//. its second argument; `false` otherwise.
//.
//. Strings are compared lexicographically. Specifically, the Unicode
//. code point value of each character in the first string is compared
//. to the value of the corresponding character in the second string.
//.
//. See also [`lt`](#lt), [`gt`](#gt), and [`gte`](#gte).
//.
//. ```javascript
//. > S.lte(10, 2)
//. false
//.
//. > S.lte('10', '2')
//. true
//. ```
function lte(x, y) {
return x <= y;
}
S.lte = def('lte', {a: [Ord]}, [a, a, $.Boolean], lte);

//# gt :: Ord a => a -> a -> Boolean
//.
//. Returns `true` if its first argument is larger than its second
//. argument; `false` otherwise.
//.
//. Strings are compared lexicographically. Specifically, the Unicode
//. code point value of each character in the first string is compared
//. to the value of the corresponding character in the second string.
//.
//. See also [`gte`](#gte), [`lt`](#lt), and [`lte`](#lte).
//.
//. ```javascript
//. > S.gt(10, 2)
//. true
//.
//. > S.gt('10', '2')
//. false
//. ```
function gt(x, y) {
return x > y;
}
S.gt = def('gt', {a: [Ord]}, [a, a, $.Boolean], gt);

//# gte :: Ord a => a -> a -> Boolean
//.
//. Returns `true` if its first argument is larger than or equal to
//. its second argument; `false` otherwise.
//.
//. Strings are compared lexicographically. Specifically, the Unicode
//. code point value of each character in the first string is compared
//. to the value of the corresponding character in the second string.
//.
//. See also [`gt`](#gt), [`lt`](#lt), and [`lte`](#lte).
//.
//. ```javascript
//. > S.gte(10, 2)
//. true
//.
//. > S.gte('10', '2')
//. false
//. ```
function gte(x, y) {
return x >= y;
}
S.gte = def('gte', {a: [Ord]}, [a, a, $.Boolean], gte);

//# min :: Ord a => a -> a -> a
//.
//. Returns the smaller of its two arguments.
Expand Down
55 changes: 55 additions & 0 deletions test/gt.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
'use strict';

var S = require('..');

var eq = require('./internal/eq');
var throws = require('./internal/throws');


test('gt', function() {

eq(typeof S.gt, 'function');
eq(S.gt.length, 2);

throws(function() { S.gt(null); },
TypeError,
'Type-class constraint violation\n' +
'\n' +
'gt :: Ord a => a -> a -> Boolean\n' +
' ^^^^^ ^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'\n' +
'‘gt’ requires ‘a’ to satisfy the Ord type-class constraint; the value at position 1 does not.\n');

throws(function() { S.gt('abc', 123); },
TypeError,
'Type-variable constraint violation\n' +
'\n' +
'gt :: Ord a => a -> a -> Boolean\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) "abc" :: String\n' +
'\n' +
'2) 123 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n');

eq(S.gt(0, 0), false);
eq(S.gt(0, -0), false);
eq(S.gt(-0, 0), false);
eq(S.gt(-0, -0), false);
eq(S.gt(0, 1), false);
eq(S.gt(1, 0), true);
eq(S.gt(0, -1), true);
eq(S.gt(-1, 0), false);
eq(S.gt('a', 'a'), false);
eq(S.gt('a', 'z'), false);
eq(S.gt('z', 'a'), true);
eq(S.gt(new Date(0), new Date(0)), false);
eq(S.gt(new Date(0), new Date(1)), false);
eq(S.gt(new Date(1), new Date(0)), true);

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

var S = require('..');

var eq = require('./internal/eq');
var throws = require('./internal/throws');


test('gte', function() {

eq(typeof S.gte, 'function');
eq(S.gte.length, 2);

throws(function() { S.gte(null); },
TypeError,
'Type-class constraint violation\n' +
'\n' +
'gte :: Ord a => a -> a -> Boolean\n' +
' ^^^^^ ^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'\n' +
'‘gte’ requires ‘a’ to satisfy the Ord type-class constraint; the value at position 1 does not.\n');

throws(function() { S.gte('abc', 123); },
TypeError,
'Type-variable constraint violation\n' +
'\n' +
'gte :: Ord a => a -> a -> Boolean\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) "abc" :: String\n' +
'\n' +
'2) 123 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n');

eq(S.gte(0, 0), true);
eq(S.gte(0, -0), true);
eq(S.gte(-0, 0), true);
eq(S.gte(-0, -0), true);
eq(S.gte(0, 1), false);
eq(S.gte(1, 0), true);
eq(S.gte(0, -1), true);
eq(S.gte(-1, 0), false);
eq(S.gte('a', 'a'), true);
eq(S.gte('a', 'z'), false);
eq(S.gte('z', 'a'), true);
eq(S.gte(new Date(0), new Date(0)), true);
eq(S.gte(new Date(0), new Date(1)), false);
eq(S.gte(new Date(1), new Date(0)), true);

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

var S = require('..');

var eq = require('./internal/eq');
var throws = require('./internal/throws');


test('lt', function() {

eq(typeof S.lt, 'function');
eq(S.lt.length, 2);

throws(function() { S.lt(null); },
TypeError,
'Type-class constraint violation\n' +
'\n' +
'lt :: Ord a => a -> a -> Boolean\n' +
' ^^^^^ ^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'\n' +
'‘lt’ requires ‘a’ to satisfy the Ord type-class constraint; the value at position 1 does not.\n');

throws(function() { S.lt('abc', 123); },
TypeError,
'Type-variable constraint violation\n' +
'\n' +
'lt :: Ord a => a -> a -> Boolean\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) "abc" :: String\n' +
'\n' +
'2) 123 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n');

eq(S.lt(0, 0), false);
eq(S.lt(0, -0), false);
eq(S.lt(-0, 0), false);
eq(S.lt(-0, -0), false);
eq(S.lt(0, 1), true);
eq(S.lt(1, 0), false);
eq(S.lt(0, -1), false);
eq(S.lt(-1, 0), true);
eq(S.lt('a', 'a'), false);
eq(S.lt('a', 'z'), true);
eq(S.lt('z', 'a'), false);
eq(S.lt(new Date(0), new Date(0)), false);
eq(S.lt(new Date(0), new Date(1)), true);
eq(S.lt(new Date(1), new Date(0)), false);

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

var S = require('..');

var eq = require('./internal/eq');
var throws = require('./internal/throws');


test('lte', function() {

eq(typeof S.lte, 'function');
eq(S.lte.length, 2);

throws(function() { S.lte(null); },
TypeError,
'Type-class constraint violation\n' +
'\n' +
'lte :: Ord a => a -> a -> Boolean\n' +
' ^^^^^ ^\n' +
' 1\n' +
'\n' +
'1) null :: Null\n' +
'\n' +
'‘lte’ requires ‘a’ to satisfy the Ord type-class constraint; the value at position 1 does not.\n');

throws(function() { S.lte('abc', 123); },
TypeError,
'Type-variable constraint violation\n' +
'\n' +
'lte :: Ord a => a -> a -> Boolean\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) "abc" :: String\n' +
'\n' +
'2) 123 :: Number, FiniteNumber, NonZeroFiniteNumber, Integer, ValidNumber\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n');

eq(S.lte(0, 0), true);
eq(S.lte(0, -0), true);
eq(S.lte(-0, 0), true);
eq(S.lte(-0, -0), true);
eq(S.lte(0, 1), true);
eq(S.lte(1, 0), false);
eq(S.lte(0, -1), false);
eq(S.lte(-1, 0), true);
eq(S.lte('a', 'a'), true);
eq(S.lte('a', 'z'), true);
eq(S.lte('z', 'a'), false);
eq(S.lte(new Date(0), new Date(0)), true);
eq(S.lte(new Date(0), new Date(1)), true);
eq(S.lte(new Date(1), new Date(0)), false);

});

0 comments on commit d6b3822

Please sign in to comment.