Skip to content

Commit

Permalink
maybe: change type of S.Nothing from ‘-> Maybe a’ to ‘Maybe a’
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed May 31, 2016
1 parent 93f6d9f commit 71a307e
Show file tree
Hide file tree
Showing 51 changed files with 382 additions and 395 deletions.
291 changes: 140 additions & 151 deletions index.js

Large diffs are not rendered by default.

10 changes: 5 additions & 5 deletions test/Maybe/Just.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Just', function() {

it('provides an "ap" method', function() {
eq(S.Just(S.inc).ap.length, 1);
eq(S.Just(S.inc).ap(S.Nothing()), S.Nothing());
eq(S.Just(S.inc).ap(S.Nothing), S.Nothing);
eq(S.Just(S.inc).ap(S.Just(42)), S.Just(43));

throws(function() { S.Just(S.inc).ap([1, 2, 3]); },
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('Just', function() {

it('provides a "concat" method', function() {
eq(S.Just('foo').concat.length, 1);
eq(S.Just('foo').concat(S.Nothing()), S.Just('foo'));
eq(S.Just('foo').concat(S.Nothing), S.Just('foo'));
eq(S.Just('foo').concat(S.Just('bar')), S.Just('foobar'));

throws(function() { S.Just('foo').concat([1, 2, 3]); },
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('Just', function() {
eq(S.Just(42).equals.length, 1);
eq(S.Just(42).equals(S.Just(42)), true);
eq(S.Just(42).equals(S.Just(43)), false);
eq(S.Just(42).equals(S.Nothing()), false);
eq(S.Just(42).equals(S.Nothing), false);
eq(S.Just(42).equals(null), false);

// Value-based equality:
Expand Down Expand Up @@ -153,9 +153,9 @@ describe('Just', function() {
it('provides a "filter" method', function() {
eq(S.Just(42).filter.length, 1);
eq(S.Just(42).filter(R.T), S.Just(42));
eq(S.Just(42).filter(R.F), S.Nothing());
eq(S.Just(42).filter(R.F), S.Nothing);
eq(S.Just(42).filter(function(n) { return n > 0; }), S.Just(42));
eq(S.Just(42).filter(function(n) { return n < 0; }), S.Nothing());
eq(S.Just(42).filter(function(n) { return n < 0; }), S.Nothing);

var m = S.Just(-5);
var f = function(n) { return n * n; };
Expand Down
2 changes: 1 addition & 1 deletion test/Maybe/Maybe.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var IdentityArb = function(arb) {

// MaybeArb :: Arbitrary a -> Arbitrary (Maybe a)
var MaybeArb = function(arb) {
return jsc.oneof(JustArb(arb), jsc.constant(S.Nothing()));
return jsc.oneof(JustArb(arb), jsc.constant(S.Nothing));
};

// JustArb :: Arbitrary a -> Arbitrary (Maybe a)
Expand Down
2 changes: 1 addition & 1 deletion test/Maybe/MaybeType.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var S = require('../..');
describe('MaybeType', function() {

it('has its type definition exported', function() {
eq($.test($.env, S.MaybeType($.Number), S.Nothing()), true);
eq($.test($.env, S.MaybeType($.Number), S.Nothing), true);
eq($.test($.env, S.MaybeType($.Number), S.Just(42)), true);
eq($.test($.env, S.MaybeType($.Number), S.Just('42')), false);
eq($.test($.env, S.MaybeType($.Number), S.Right(42)), false);
Expand Down
108 changes: 53 additions & 55 deletions test/Maybe/Nothing.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,18 @@ var square = require('../utils').square;

describe('Nothing', function() {

it('is a data constructor', function() {
eq(typeof S.Nothing, 'function');
eq(S.Nothing.length, 0);
eq(S.Nothing()['@@type'], 'sanctuary/Maybe');
eq(S.Nothing().isNothing, true);
eq(S.Nothing().isJust, false);
it('is a member of the "Maybe a" type', function() {
eq(S.Nothing['@@type'], 'sanctuary/Maybe');
eq(S.Nothing.isNothing, true);
eq(S.Nothing.isJust, false);
});

it('provides an "ap" method', function() {
eq(S.Nothing().ap.length, 1);
eq(S.Nothing().ap(S.Nothing()), S.Nothing());
eq(S.Nothing().ap(S.Just(42)), S.Nothing());
eq(S.Nothing.ap.length, 1);
eq(S.Nothing.ap(S.Nothing), S.Nothing);
eq(S.Nothing.ap(S.Just(42)), S.Nothing);

throws(function() { S.Nothing().ap([1, 2, 3]); },
throws(function() { S.Nothing.ap([1, 2, 3]); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -40,10 +38,10 @@ describe('Nothing', function() {
});

it('provides a "chain" method', function() {
eq(S.Nothing().chain.length, 1);
eq(S.Nothing().chain(S.head), S.Nothing());
eq(S.Nothing.chain.length, 1);
eq(S.Nothing.chain(S.head), S.Nothing);

throws(function() { S.Nothing().chain(null); },
throws(function() { S.Nothing.chain(null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -57,11 +55,11 @@ describe('Nothing', function() {
});

it('provides a "concat" method', function() {
eq(S.Nothing().concat.length, 1);
eq(S.Nothing().concat(S.Nothing()), S.Nothing());
eq(S.Nothing().concat(S.Just('foo')), S.Just('foo'));
eq(S.Nothing.concat.length, 1);
eq(S.Nothing.concat(S.Nothing), S.Nothing);
eq(S.Nothing.concat(S.Just('foo')), S.Just('foo'));

throws(function() { S.Nothing().concat(null); },
throws(function() { S.Nothing.concat(null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -73,7 +71,7 @@ describe('Nothing', function() {
'\n' +
'The value at position 1 is not a member of ‘Maybe a’.\n'));

throws(function() { S.Nothing().concat(S.Just(1)); },
throws(function() { S.Nothing.concat(S.Just(1)); },
errorEq(TypeError,
'Type-class constraint violation\n' +
'\n' +
Expand All @@ -87,24 +85,24 @@ describe('Nothing', function() {
});

it('provides an "equals" method', function() {
eq(S.Nothing().equals.length, 1);
eq(S.Nothing().equals(S.Nothing()), true);
eq(S.Nothing().equals(S.Just(42)), false);
eq(S.Nothing().equals(null), false);
eq(S.Nothing.equals.length, 1);
eq(S.Nothing.equals(S.Nothing), true);
eq(S.Nothing.equals(S.Just(42)), false);
eq(S.Nothing.equals(null), false);
});

it('provides an "extend" method', function() {
eq(S.Nothing().extend.length, 1);
eq(S.Nothing().extend(function(x) { return x.value / 2; }), S.Nothing());
eq(S.Nothing.extend.length, 1);
eq(S.Nothing.extend(function(x) { return x.value / 2; }), S.Nothing);

// associativity
var w = S.Nothing();
var w = S.Nothing;
var f = function(x) { return x.value + 1; };
var g = function(x) { return x.value * x.value; };
eq(w.extend(g).extend(f),
w.extend(function(_w) { return f(_w.extend(g)); }));

throws(function() { S.Nothing().extend(null); },
throws(function() { S.Nothing.extend(null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -118,11 +116,11 @@ describe('Nothing', function() {
});

it('provides a "filter" method', function() {
eq(S.Nothing().filter.length, 1);
eq(S.Nothing().filter(R.T), S.Nothing());
eq(S.Nothing().filter(R.F), S.Nothing());
eq(S.Nothing.filter.length, 1);
eq(S.Nothing.filter(R.T), S.Nothing);
eq(S.Nothing.filter(R.F), S.Nothing);

var m = S.Nothing();
var m = S.Nothing;
var f = function(n) { return n * n; };
var p = function(n) { return n < 0; };
var q = function(n) { return n > 0; };
Expand All @@ -132,7 +130,7 @@ describe('Nothing', function() {
assert(m.map(f).filter(q)
.equals(m.filter(function(x) { return q(f(x)); }).map(f)));

throws(function() { S.Nothing().filter(null); },
throws(function() { S.Nothing.filter(null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -146,10 +144,10 @@ describe('Nothing', function() {
});

it('provides a "map" method', function() {
eq(S.Nothing().map.length, 1);
eq(S.Nothing().map(function() { return 42; }), S.Nothing());
eq(S.Nothing.map.length, 1);
eq(S.Nothing.map(function() { return 42; }), S.Nothing);

throws(function() { S.Nothing().map(null); },
throws(function() { S.Nothing.map(null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -163,10 +161,10 @@ describe('Nothing', function() {
});

it('provides a "reduce" method', function() {
eq(S.Nothing().reduce.length, 2);
eq(S.Nothing().reduce(function(a, b) { return a + b; }, 10), 10);
eq(S.Nothing.reduce.length, 2);
eq(S.Nothing.reduce(function(a, b) { return a + b; }, 10), 10);

throws(function() { S.Nothing().reduce(null, null); },
throws(function() { S.Nothing.reduce(null, null); },
errorEq(TypeError,
'Invalid value\n' +
'\n' +
Expand All @@ -180,31 +178,31 @@ describe('Nothing', function() {
});

it('provides a "sequence" method', function() {
eq(S.Nothing().sequence.length, 1);
eq(S.Nothing().sequence(S.Either.of), S.Right(S.Nothing()));
eq(S.Nothing.sequence.length, 1);
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);
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()');
eq(S.Nothing.toString.length, 0);
eq(S.Nothing.toString(), 'Nothing');
});

it('implements Semigroup', function() {
var a = S.Nothing();
var b = S.Nothing();
var c = S.Nothing();
var a = S.Nothing;
var b = S.Nothing;
var c = S.Nothing;

// associativity
assert(a.concat(b).concat(c).equals(a.concat(b.concat(c))));
});

it('implements Monoid', function() {
var a = S.Nothing();
var a = S.Nothing;

// left identity
assert(a.empty().concat(a).equals(a));
Expand All @@ -214,7 +212,7 @@ describe('Nothing', function() {
});

it('implements Functor', function() {
var a = S.Nothing();
var a = S.Nothing;
var f = S.inc;
var g = square;

Expand All @@ -226,9 +224,9 @@ describe('Nothing', function() {
});

it('implements Apply', function() {
var a = S.Nothing();
var b = S.Nothing();
var c = S.Nothing();
var a = S.Nothing;
var b = S.Nothing;
var c = S.Nothing;

// composition
assert(a.map(function(f) {
Expand All @@ -241,8 +239,8 @@ describe('Nothing', function() {
});

it('implements Applicative', function() {
var a = S.Nothing();
var b = S.Nothing();
var a = S.Nothing;
var b = S.Nothing;
var f = S.inc;
var x = 7;

Expand All @@ -257,7 +255,7 @@ describe('Nothing', function() {
});

it('implements Chain', function() {
var a = S.Nothing();
var a = S.Nothing;
var f = S.head;
var g = S.last;

Expand All @@ -267,7 +265,7 @@ describe('Nothing', function() {
});

it('implements Monad', function() {
var a = S.Nothing();
var a = S.Nothing;
var f = S.head;
var x = [1, 2, 3];

Expand Down
6 changes: 3 additions & 3 deletions test/and.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ describe('and', function() {
});

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.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));
});

Expand Down
8 changes: 4 additions & 4 deletions test/at.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ describe('at', function() {
eq(S.at(-1, ['foo', 'bar', 'baz']), S.Just('baz'));
});

it('returns a Nothing if index out of bounds', function() {
eq(S.at(3, ['foo', 'bar', 'baz']), S.Nothing());
eq(S.at(-4, ['foo', 'bar', 'baz']), S.Nothing());
eq(S.at(-0, ['foo', 'bar', 'baz']), S.Nothing());
it('returns Nothing if index out of bounds', function() {
eq(S.at(3, ['foo', 'bar', 'baz']), S.Nothing);
eq(S.at(-4, ['foo', 'bar', 'baz']), S.Nothing);
eq(S.at(-0, ['foo', 'bar', 'baz']), S.Nothing);
});

it('is curried', function() {
Expand Down
6 changes: 3 additions & 3 deletions test/concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ describe('concat', function() {
});

it('can be applied to maybes', function() {
eq(S.concat(S.Nothing(), S.Nothing()), S.Nothing());
eq(S.concat(S.Just('foo'), S.Nothing()), S.Just('foo'));
eq(S.concat(S.Nothing(), S.Just('bar')), S.Just('bar'));
eq(S.concat(S.Nothing, S.Nothing), S.Nothing);
eq(S.concat(S.Just('foo'), S.Nothing), S.Just('foo'));
eq(S.concat(S.Nothing, S.Just('bar')), S.Just('bar'));
eq(S.concat(S.Just('foo'), S.Just('bar')), S.Just('foobar'));
});

Expand Down
18 changes: 9 additions & 9 deletions test/drop.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ describe('drop', function() {
'The value at position 1 is not a member of ‘List a’.\n'));
});

it('returns a Nothing if n is greater than collection length', function() {
eq(S.drop(6, ['a', 'b', 'c', 'd', 'e']), S.Nothing());
eq(S.drop(6, 'abcde'), S.Nothing());
it('returns Nothing if n is greater than collection length', function() {
eq(S.drop(6, ['a', 'b', 'c', 'd', 'e']), S.Nothing);
eq(S.drop(6, 'abcde'), S.Nothing);
});

it('returns a Nothing if n is negative', function() {
eq(S.drop(-3, ['a', 'b', 'c', 'd', 'e']), S.Nothing());
eq(S.drop(-0, ['a', 'b', 'c', 'd', 'e']), S.Nothing());
eq(S.drop(-3, 'abcde'), S.Nothing());
eq(S.drop(-0, 'abcde'), S.Nothing());
eq(S.drop(new Number(-0), ['a', 'b', 'c', 'd', 'e']), S.Nothing());
it('returns Nothing if n is negative', function() {
eq(S.drop(-3, ['a', 'b', 'c', 'd', 'e']), S.Nothing);
eq(S.drop(-0, ['a', 'b', 'c', 'd', 'e']), S.Nothing);
eq(S.drop(-3, 'abcde'), S.Nothing);
eq(S.drop(-0, 'abcde'), S.Nothing);
eq(S.drop(new Number(-0), ['a', 'b', 'c', 'd', 'e']), S.Nothing);
});

it('returns an empty collection if n is equal to collection length', function() {
Expand Down
Loading

0 comments on commit 71a307e

Please sign in to comment.