Skip to content

Commit

Permalink
Merge pull request #140 from plaid/dc-even-odd
Browse files Browse the repository at this point in the history
integer: add S.even and S.odd
  • Loading branch information
davidchambers committed Feb 1, 2016
2 parents ef24f63 + 2818639 commit 445aae5
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 26 deletions.
74 changes: 48 additions & 26 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,6 @@

var S = {};

var MAX_SAFE_INTEGER = Math.pow(2, 53) - 1;
var MIN_SAFE_INTEGER = -MAX_SAFE_INTEGER;

var _ = R.__;

var sentinel = {};
Expand Down Expand Up @@ -193,24 +190,13 @@
function(either) { return either.isRight ? [either.value] : []; }
);

// Integer :: Type
var Integer = $.NullaryType(
'sanctuary/Integer',
function(x) {
return R.type(x) === 'Number' &&
Math.floor(x) === Number(x) &&
x >= MIN_SAFE_INTEGER &&
x <= MAX_SAFE_INTEGER;
}
);

// List :: Type -> Type
var List = $.UnaryType(
'sanctuary/List',
function(x) {
return x != null &&
R.type(x) !== 'Function' &&
Integer.test(x.length) &&
$.Integer.test(x.length) &&
x.length >= 0;
},
function(list) {
Expand Down Expand Up @@ -241,7 +227,7 @@
$.FiniteNumber,
$.NonZeroFiniteNumber,
$Either,
Integer,
$.Integer,
List,
$Maybe,
$.RegexFlags,
Expand Down Expand Up @@ -1805,7 +1791,7 @@
var slice = S.slice =
def('slice',
{},
[Integer, Integer, List(a), $Maybe(List(a))],
[$.Integer, $.Integer, List(a), $Maybe(List(a))],
function(start, end, xs) {
var len = xs.length;
var A = negativeZero(start) ? len : start < 0 ? start + len : start;
Expand Down Expand Up @@ -1835,7 +1821,7 @@
var at = S.at =
def('at',
{},
[Integer, List(a), $Maybe(a)],
[$.Integer, List(a), $Maybe(a)],
function(n, xs) {
return R.map(R.head, slice(n, n === -1 ? -0 : n + 1, xs));
});
Expand Down Expand Up @@ -1934,7 +1920,7 @@
S.take =
def('take',
{},
[Integer, List(a), $Maybe(List(a))],
[$.Integer, List(a), $Maybe(List(a))],
function(n, xs) {
return n < 0 || negativeZero(n) ? Nothing() : slice(0, n, xs);
});
Expand All @@ -1959,7 +1945,7 @@
S.takeLast =
def('takeLast',
{},
[Integer, List(a), $Maybe(List(a))],
[$.Integer, List(a), $Maybe(List(a))],
function(n, xs) {
return n < 0 || negativeZero(n) ? Nothing() : slice(-n, -0, xs);
});
Expand All @@ -1984,7 +1970,7 @@
S.drop =
def('drop',
{},
[Integer, List(a), $Maybe(List(a))],
[$.Integer, List(a), $Maybe(List(a))],
function(n, xs) {
return n < 0 || negativeZero(n) ? Nothing() : slice(n, -0, xs);
});
Expand All @@ -2009,7 +1995,7 @@
S.dropLast =
def('dropLast',
{},
[Integer, List(a), $Maybe(List(a))],
[$.Integer, List(a), $Maybe(List(a))],
function(n, xs) {
return n < 0 || negativeZero(n) ? Nothing() : slice(0, -n, xs);
});
Expand Down Expand Up @@ -2045,15 +2031,15 @@
function(x) {
return x != null &&
typeof x !== 'function' &&
Integer.test(x.length) &&
$.Integer.test(x.length) &&
x.length >= 0;
}
);

var sanctifyIndexOf = function(name) {
return def(name,
{b: [ArrayLike]},
[a, b, $Maybe(Integer)],
[a, b, $Maybe($.Integer)],
R.pipe(R[name], Just, R.filter(R.gte(_, 0))));
};

Expand Down Expand Up @@ -2325,6 +2311,42 @@
[$.FiniteNumber, $.NonZeroFiniteNumber, $.FiniteNumber],
function(a, b) { return a / b; });

//. ### Integer

//# even :: Integer -> Boolean
//.
//. Returns `true` if the given integer is even; `false` if it is odd.
//.
//. ```javascript
//. > S.even(42)
//. true
//.
//. > S.even(99)
//. false
//. ```
S.even =
def('even',
{},
[$.Integer, $.Boolean],
function(n) { return n % 2 === 0; });

//# odd :: Integer -> Boolean
//.
//. Returns `true` if the given integer is odd; `false` if it is even.
//.
//. ```javascript
//. > S.odd(99)
//. true
//.
//. > S.odd(42)
//. false
//. ```
S.odd =
def('odd',
{},
[$.Integer, $.Boolean],
function(n) { return n % 2 !== 0; });

//. ### Parse

//# parseDate :: String -> Maybe Date
Expand Down Expand Up @@ -2424,7 +2446,7 @@
S.parseInt =
def('parseInt',
{},
[Integer, $.String, $Maybe(Integer)],
[$.Integer, $.String, $Maybe($.Integer)],
function(radix, s) {
if (radix < 2 || radix > 36) {
throw new RangeError('Radix not in [2 .. 36]');
Expand All @@ -2441,7 +2463,7 @@
R.indexOf(_, charset),
R.gte(_, 0))))),
R.map(R.partialRight(parseInt, [radix])),
R.filter(Integer.test)
R.filter($.Integer.test)
)(s);
});

Expand Down
82 changes: 82 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3514,6 +3514,88 @@ describe('number', function() {

});

describe('integer', function() {

describe('even', function() {

it('is a unary function', function() {
eq(typeof S.even, 'function');
eq(S.even.length, 1);
});

it('type checks its arguments', function() {
assert.throws(function() { S.even(0.5); },
errorEq(TypeError,
'‘even’ expected a value of type Integer ' +
'as its first argument; received 0.5'));

assert.throws(function() { S.even(Infinity); },
errorEq(TypeError,
'‘even’ expected a value of type Integer ' +
'as its first argument; received Infinity'));
});

it('returns true for even integer', function() {
eq(S.even(0), true);
eq(S.even(-0), true);
eq(S.even(2), true);
eq(S.even(-2), true);
eq(S.even(new Number(0)), true);
eq(S.even(new Number(-0)), true);
eq(S.even(new Number(2)), true);
eq(S.even(new Number(-2)), true);
});

it('returns false for odd integer', function() {
eq(S.even(1), false);
eq(S.even(-1), false);
eq(S.even(new Number(1)), false);
eq(S.even(new Number(-1)), false);
});

});

describe('odd', function() {

it('is a unary function', function() {
eq(typeof S.odd, 'function');
eq(S.odd.length, 1);
});

it('type checks its arguments', function() {
assert.throws(function() { S.odd(-0.5); },
errorEq(TypeError,
'‘odd’ expected a value of type Integer ' +
'as its first argument; received -0.5'));

assert.throws(function() { S.odd(-Infinity); },
errorEq(TypeError,
'‘odd’ expected a value of type Integer ' +
'as its first argument; received -Infinity'));
});

it('returns true for odd integer', function() {
eq(S.odd(1), true);
eq(S.odd(-1), true);
eq(S.odd(new Number(1)), true);
eq(S.odd(new Number(-1)), true);
});

it('returns false for even integer', function() {
eq(S.odd(0), false);
eq(S.odd(-0), false);
eq(S.odd(2), false);
eq(S.odd(-2), false);
eq(S.odd(new Number(0)), false);
eq(S.odd(new Number(-0)), false);
eq(S.odd(new Number(2)), false);
eq(S.odd(new Number(-2)), false);
});

});

});

describe('parse', function() {

describe('parseDate', function() {
Expand Down

0 comments on commit 445aae5

Please sign in to comment.