Skip to content

Commit

Permalink
maybe: define "filter"
Browse files Browse the repository at this point in the history
  • Loading branch information
davidchambers committed Feb 13, 2015
1 parent 705fde6 commit 9f7efec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
10 changes: 10 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
return maybe instanceof Nothing;
};

// Nothing#filter :: (a -> Boolean) -> Maybe a
Nothing.prototype.filter = function(pred) { // jshint ignore:line
return this;
};

// Nothing#or :: Maybe a -> Maybe a
Nothing.prototype.or = function(maybe) {
return maybe;
Expand Down Expand Up @@ -94,6 +99,11 @@
return maybe instanceof Just && maybe.value === this.value;
};

// Just#filter :: (a -> Boolean) -> Maybe a
Just.prototype.filter = function(pred) {
return pred(this.value) ? this : Nothing();
};

// Just#or :: Maybe a -> Maybe a
Just.prototype.or = function(maybe) { // jshint ignore:line
return this;
Expand Down
44 changes: 41 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ var S = require('..');

var eq = assert.strictEqual;

var T = function() { return true; };
var F = function() { return false; };


describe('maybe', function() {

Expand Down Expand Up @@ -61,6 +64,24 @@ describe('maybe', function() {
eq(nothing.equals(S.Just(42)), false);
});

it('provides a "filter" method', function() {
var nothing = S.Nothing();
eq(nothing.filter.length, 1);
eq(nothing.filter(T), nothing);
eq(nothing.filter(F), 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; };

// FIXME: What is the name of this law?
assert(m.map(f).filter(p)
.equals(m.filter(function(x) { return p(f(x)); }).map(f)));
assert(m.map(f).filter(q)
.equals(m.filter(function(x) { return q(f(x)); }).map(f)));
});

it('provides a "map" method', function() {
var nothing = S.Nothing();
eq(nothing.map.length, 1);
Expand Down Expand Up @@ -114,6 +135,26 @@ describe('maybe', function() {
eq(just.equals(S.Nothing()), false);
});

it('provides a "filter" method', function() {
var just = S.Just(42);
eq(just.filter.length, 1);
eq(just.filter(T), just);
eq(just.filter(F).equals(S.Nothing()), true);
eq(just.filter(function(n) { return n > 0; }), just);
eq(just.filter(function(n) { return n < 0; }).equals(S.Nothing()), true);

var m = S.Just(-5);
var f = function(n) { return n * n; };
var p = function(n) { return n < 0; };
var q = function(n) { return n > 0; };

// FIXME: What is the name of this law?
assert(m.map(f).filter(p)
.equals(m.filter(function(x) { return p(f(x)); }).map(f)));
assert(m.map(f).filter(q)
.equals(m.filter(function(x) { return q(f(x)); }).map(f)));
});

it('provides a "map" method', function() {
var just = S.Just(42);
eq(just.map.length, 1);
Expand Down Expand Up @@ -480,9 +521,6 @@ describe('list', function() {

describe('find', function() {

var T = function() { return true; };
var F = function() { return false; };

it('returns Just the first element satisfying the predicate', function() {
assert(S.find(T, [null]).equals(S.Just(null)));
assert(S.find(function(n) { return n >= 0; }, [-1, 0, 1])
Expand Down

0 comments on commit 9f7efec

Please sign in to comment.