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

add support for the contains() function, fixes #88 #93

Merged
merged 3 commits into from
Jan 2, 2017

Conversation

emmanueltouzery
Copy link
Collaborator

No description provided.

return Return(false)
}
var h = l.head()
return h === val ?
Copy link
Member

@ulfryk ulfryk Jan 2, 2017

Choose a reason for hiding this comment

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

I think that we should add some helper function so we can use .equals() powers of our structures. It'll also work with other 3rd party stuff that implements Setoid.

See Immutable.JS is()

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

good point! I wasn't aware of this Setoid initiative. Nice to see that. So, we would have something like...

if (a.equals && b.equals) {
    return a.equals(b);
}
return a === b;

?

not sure how it'll work for immutablejs if they don't implement Setoid though :( Maybe a user would have to add equals to their prototypes? Didn't check how their collections are implemented. Also, I guess that won't work with JS arrays out of the box?

Copy link
Member

Choose a reason for hiding this comment

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

Immutable has quite sophisticated implementation of is-> https://github.com/facebook/immutable-js/blob/master/src/is.js

but I think we don't need that much, I think that sth like this would be enough:

  if (a === b || (a !== a && b !== b)) {
    return true;
  }
  if (!a || !b) {
    return false;
  }
  if (isFunction(a.equals) && isFunction(b.equals)) {
    return a.equals(b);
  }
  return false;

The a !== a && b !== b is about NaN.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm you surely have thought about this more than I did, but the if (!a || !b) frightens me. It makes it that "" != "", because !"" is true. Maybe we should just do === undefined or what it is you have in mind there?

Copy link
Member

Choose a reason for hiding this comment

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

Look that if we have a = "" and b = "" it'll be returned with true from first condition.

This line is an optimisation to avoid function checks.

If you don't like it it can be dropped, but it's not that evil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ok i understand now. let's add this, i'll just add comments. too bad we don't (afaict) don't cover arrays though

Copy link
Member

Choose a reason for hiding this comment

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

one can always use our or immutable's is():

arr.some(a => is(a, b))

Or we can make it curried so:

arr.some(is(a))

;)

Copy link
Member

Choose a reason for hiding this comment

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

not sure how it'll work for immutablejs if they don't implement Setoid though :( Maybe a user would have to add equals to their prototypes?

I missed this part somehow. All ImmutableJS structures implement Setoid ;)

@@ -627,6 +649,9 @@
return fn(a) ? self : None()
})
},
contains: function (val) {
return this.isSome() ? this.val === val : false;
Copy link
Member

Choose a reason for hiding this comment

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

...and use this helper to all equality checks.

@emmanueltouzery
Copy link
Collaborator Author

done. I didn't squash the commits. Maybe it could make sense to export 'areEqual' to users of the library, not sure...

ulfryk
ulfryk previously approved these changes Jan 2, 2017
@ulfryk ulfryk dismissed their stale review January 2, 2017 16:05

Please run npm run publ and commit changed dist/ :)

@emmanueltouzery
Copy link
Collaborator Author

sorry :S when this is merged i'll rebase the forEach pull request and run publ there too

@ulfryk ulfryk merged commit 178d9ef into monet:develop Jan 2, 2017
@ulfryk ulfryk mentioned this pull request Jan 2, 2017
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.

None yet

2 participants