Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logic: make S.and and S.or monomorphic, and remove S.xor #279

Merged
merged 1 commit into from
Nov 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
159 changes: 32 additions & 127 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,15 +284,6 @@
}
);

// Monoid :: TypeClass
var Monoid = $.TypeClass(
'sanctuary/Monoid',
function(x) {
return R.contains(R.type(x), ['Array', 'Boolean', 'String']) ||
hasMethod('empty')(x);
}
);

// Ord :: TypeClass
var Ord = $.TypeClass(
'sanctuary/Ord',
Expand Down Expand Up @@ -1096,26 +1087,6 @@
[$Maybe(a), $.Function, b],
Maybe$prototype$sequence);

//# Maybe#toBoolean :: Maybe a ~> () -> Boolean
//.
//. Returns `false` if `this` is Nothing; `true` if `this` is a Just.
//.
//. ```javascript
//. > S.Nothing.toBoolean()
//. false
//.
//. > S.Just(42).toBoolean()
//. true
//. ```
function Maybe$prototype$toBoolean(self) {
return self.isJust;
}
Maybe.prototype.toBoolean =
method('Maybe#toBoolean',
{},
[$Maybe(a), $.Boolean],
Maybe$prototype$toBoolean);

//# Maybe#toString :: Maybe a ~> () -> String
//.
//. Returns the string representation of the Maybe.
Expand Down Expand Up @@ -1704,26 +1675,6 @@
[$Either(a, b), $.Function, c],
Either$prototype$sequence);

//# Either#toBoolean :: Either a b ~> () -> Boolean
//.
//. Returns `false` if `this` is a Left; `true` if `this` is a Right.
//.
//. ```javascript
//. > S.Left(42).toBoolean()
//. false
//.
//. > S.Right(42).toBoolean()
//. true
//. ```
function Either$prototype$toBoolean(self) {
return self.isRight;
}
Either.prototype.toBoolean =
method('Either#toBoolean',
{},
[$Either(a, b), $.Boolean],
Either$prototype$toBoolean);

//# Either#toString :: Either a b ~> () -> String
//.
//. Returns the string representation of the Either.
Expand Down Expand Up @@ -2033,108 +1984,62 @@
S.eitherToMaybe =
def('eitherToMaybe', {}, [$Either(a, b), $Maybe(b)], eitherToMaybe);

//. ### Alternative

// Alternative :: TypeClass
var Alternative = $.TypeClass(
'Alternative',
function(x) {
return R.contains(R.type(x), ['Array', 'Boolean']) ||
hasMethod('toBoolean')(x);
}
);

// toBoolean :: Alternative a => a -> Boolean
var toBoolean = function(x) {
switch (R.type(x)) {
case 'Array': return x.length > 0;
case 'Boolean': return x.valueOf();
default: return x.toBoolean();
}
};

// empty :: Monoid a => a -> a
var empty = function(x) {
switch (R.type(x)) {
case 'Array': return [];
case 'Boolean': return false;
default: return x.empty();
}
};
//. ### Logic

//# and :: Alternative a => a -> a -> a
//# and :: Boolean -> Boolean -> Boolean
//.
//. Takes two values of the same type and returns the second value
//. if the first is "true"; the first value otherwise. An array is
//. considered "true" if its length is greater than zero. The Boolean
//. value `true` is also considered "true". Other types must provide
//. a `toBoolean` method.
//. Boolean "and".
//.
//. ```javascript
//. > S.and(S.Just(1), S.Just(2))
//. Just(2)
//. > S.and(false, false)
//. false
//.
//. > S.and(S.Nothing, S.Just(3))
//. Nothing
//. > S.and(false, true)
//. false
//.
//. > S.and(true, false)
//. false
//.
//. > S.and(true, true)
//. true
//. ```
function and(x, y) {
return toBoolean(x) ? y : x;
return x.valueOf() && y.valueOf();
}
S.and = def('and', {a: [Alternative]}, [a, a, a], and);
S.and = def('and', {}, [$.Boolean, $.Boolean, $.Boolean], and);

//# or :: Alternative a => a -> a -> a
//# or :: Boolean -> Boolean -> Boolean
//.
//. Takes two values of the same type and returns the first value if it
//. is "true"; the second value otherwise. An array is considered "true"
//. if its length is greater than zero. The Boolean value `true` is also
//. considered "true". Other types must provide a `toBoolean` method.
//. Boolean "or".
//.
//. ```javascript
//. > S.or(S.Just(1), S.Just(2))
//. Just(1)
//.
//. > S.or(S.Nothing, S.Just(3))
//. Just(3)
//. ```
function or(x, y) {
return toBoolean(x) ? x : y;
}
S.or = def('or', {a: [Alternative]}, [a, a, a], or);

//# xor :: (Alternative a, Monoid a) => a -> a -> a
//. > S.or(false, false)
//. false
//.
//. Takes two values of the same type and returns the "true" value
//. if one value is "true" and the other is "false"; otherwise it
//. returns the type's "false" value. An array is considered "true"
//. if its length is greater than zero. The Boolean value `true` is
//. also considered "true". Other types must provide `toBoolean` and
//. `empty` methods.
//. > S.or(false, true)
//. true
//.
//. ```javascript
//. > S.xor(S.Nothing, S.Just(1))
//. Just(1)
//. > S.or(true, false)
//. true
//.
//. > S.xor(S.Just(2), S.Just(3))
//. Nothing
//. > S.or(true, true)
//. true
//. ```
function xor(x, y) {
return toBoolean(x) === toBoolean(y) ? empty(x) : or(x, y);
function or(x, y) {
return x.valueOf() || y.valueOf();
}
S.xor = def('xor', {a: [Alternative, Monoid]}, [a, a, a], xor);

//. ### Logic
S.or = def('or', {}, [$.Boolean, $.Boolean, $.Boolean], or);

//# not :: Boolean -> Boolean
//.
//. Takes a Boolean and returns the negation of that value
//. (`false` for `true`; `true` for `false`).
//. Boolean "not".
//.
//. ```javascript
//. > S.not(true)
//. false
//.
//. > S.not(false)
//. true
//.
//. > S.not(true)
//. false
//. ```
function not(x) {
return !x.valueOf();
Expand Down
5 changes: 0 additions & 5 deletions test/Either/Left.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,6 @@ describe('Left', function() {
eq(S.Left('abc').sequence(S.Maybe.of), S.Just(S.Left('abc')));
});

it('provides a "toBoolean" method', function() {
eq(S.Left('abc').toBoolean.length, 0);
eq(S.Left('abc').toBoolean(), false);
});

it('provides a "toString" method', function() {
eq(S.Left('abc').toString.length, 0);
eq(S.Left('abc').toString(), 'Left("abc")');
Expand Down
5 changes: 0 additions & 5 deletions test/Either/Right.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,6 @@ describe('Right', function() {
eq(S.Right(S.Just(42)).sequence(S.Maybe.of), S.Just(S.Right(42)));
});

it('provides a "toBoolean" method', function() {
eq(S.Right(42).toBoolean.length, 0);
eq(S.Right(42).toBoolean(), true);
});

it('provides a "toString" method', function() {
eq(S.Right([1, 2, 3]).toString.length, 0);
eq(S.Right([1, 2, 3]).toString(), 'Right([1, 2, 3])');
Expand Down
5 changes: 0 additions & 5 deletions test/Maybe/Just.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,6 @@ describe('Just', function() {
eq(S.Just(S.Right(42)).sequence(S.Either.of), S.Right(S.Just(42)));
});

it('provides a "toBoolean" method', function() {
eq(S.Just(42).toBoolean.length, 0);
eq(S.Just(42).toBoolean(), true);
});

it('provides a "toString" method', function() {
eq(S.Just([1, 2, 3]).toString.length, 0);
eq(S.Just([1, 2, 3]).toString(), 'Just([1, 2, 3])');
Expand Down
5 changes: 0 additions & 5 deletions test/Maybe/Nothing.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,6 @@ describe('Nothing', function() {
eq(S.Nothing.sequence(S.Either.of), S.Right(S.Nothing));
});

it('provides a "toBoolean" method', function() {
eq(S.Nothing.toBoolean.length, 0);
eq(S.Nothing.toBoolean(), false);
});

it('provides a "toString" method', function() {
eq(S.Nothing.toString.length, 0);
eq(S.Nothing.toString(), 'Nothing');
Expand Down
76 changes: 5 additions & 71 deletions test/and.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
'use strict';

var throws = require('assert').throws;

var R = require('ramda');

var S = require('..');

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


describe('and', function() {
Expand All @@ -17,76 +12,15 @@ describe('and', function() {
eq(S.and.length, 2);
});

it('can be applied to Booleans', function() {
it('has && semantics', function() {
eq(S.and(false, false), false);
eq(S.and(false, true), false);
eq(S.and(true, false), false);
eq(S.and(true, true), true);
});

it('can be applied to arrays', function() {
eq(S.and([], []), []);
eq(S.and([], [42]), []);
eq(S.and([42], []), []);
eq(S.and([42], [43]), [43]);
});

it('can be applied to maybes', function() {
eq(S.and(S.Nothing, S.Nothing), S.Nothing);
eq(S.and(S.Nothing, S.Just(42)), S.Nothing);
eq(S.and(S.Just(42), S.Nothing), S.Nothing);
eq(S.and(S.Just(42), S.Just(43)), S.Just(43));
});

it('can be applied to eithers', function() {
eq(S.and(S.Left('foo'), S.Left('bar')), S.Left('foo'));
eq(S.and(S.Left('foo'), S.Right(42)), S.Left('foo'));
eq(S.and(S.Right(42), S.Left('foo')), S.Left('foo'));
eq(S.and(S.Right(42), S.Right(43)), S.Right(43));
});

it('throws if applied to values of different types', function() {
throws(function() { S.and([], false); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'and :: Alternative a => a -> a -> a\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) [] :: Array ???\n' +
'\n' +
'2) false :: Boolean\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));

throws(function() { S.and(R.__, false)([]); },
errorEq(TypeError,
'Type-variable constraint violation\n' +
'\n' +
'and :: Alternative a => a -> a -> a\n' +
' ^ ^\n' +
' 1 2\n' +
'\n' +
'1) [] :: Array ???\n' +
'\n' +
'2) false :: Boolean\n' +
'\n' +
'Since there is no type of which all the above values are members, the type-variable constraint has been violated.\n'));
});

it('throws if applied to values without a "toBoolean" method', function() {
throws(function() { S.and(0, 1); },
errorEq(TypeError,
'Type-class constraint violation\n' +
'\n' +
'and :: Alternative a => a -> a -> a\n' +
' ^^^^^^^^^^^^^ ^\n' +
' 1\n' +
'\n' +
'1) 0 :: Number, FiniteNumber, Integer, ValidNumber\n' +
'\n' +
'‘and’ requires ‘a’ to satisfy the Alternative type-class constraint; the value at position 1 does not.\n'));
eq(S.and(new Boolean(false), new Boolean(false)), false);
eq(S.and(new Boolean(false), new Boolean(true)), false);
eq(S.and(new Boolean(true), new Boolean(false)), false);
eq(S.and(new Boolean(true), new Boolean(true)), true);
});

});
Loading