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

maybe: define "filter" #10

Merged
merged 1 commit into from
Mar 1, 2015
Merged

maybe: define "filter" #10

merged 1 commit into from
Mar 1, 2015

Conversation

davidchambers
Copy link
Member

This makes Maybe compatible with R.filter and, if ramda/ramda#799 is merged, with R.reject.

@joneshf, would you mind reviewing this to make sure I faithfully translated the first of the laws you gave me?

var p = function(n) { return n < 0; };
var q = function(n) { return n > 0; };

// FIXME: What is the name of this law?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is actually a free theorem by parametricity.

@joneshf
Copy link
Member

joneshf commented Feb 13, 2015

Looks like a good implementation.

I'm not sure what your overall idea is here, but I'd like to think that you could implement filter once and for all if you know something is both a Monad and a Monoid:

var filter = function(p, m) {
  return m.chain(function(x) {
    return p(x) ? m.of(x) : m.empty();
  });
}

Then you could put this on the prototype of whatever.

...
Maybe.prototype.filter = function(p) {
  return filter(p, this);
}
...

So you don't end up having to write something similar for other types that are Monad and Monoid. If you're not planning on using the fantasyland stuff then forget about it.

@davidchambers
Copy link
Member Author

I'd like to think that you could implement filter once and for all if you know something is both a Monad and a Monoid

Great idea! Once I merge #8 Maybe will implement the Monad specification. I'll then open a pull request to have it implement the Monoid specification. Once I merge that pull request I'll update this one to use your filter function. :)

@davidchambers
Copy link
Member Author

I've updated this pull request. Having Nothing and Just share their definition of filter is wonderful!

davidchambers added a commit that referenced this pull request Mar 1, 2015
@davidchambers davidchambers merged commit d4d3b5a into master Mar 1, 2015
@davidchambers davidchambers deleted the dc-filter branch March 1, 2015 18:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants