Skip to content

use Ord for nub #128

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

Closed
wants to merge 3 commits into from
Closed

Conversation

matthewleon
Copy link
Contributor

On JS engines where Array.push is amortized O(1) (I believe this to be common), this should be amortized O(n*logn).

@matthewleon
Copy link
Contributor Author

Actually this should always be O(n*logn), as the array sort should provide the upper bound.

@matthewleon
Copy link
Contributor Author

Thought of a simpler technique, will update this.

@matthewleon matthewleon force-pushed the ordNub branch 2 times, most recently from 7f9dcbf to 2b85642 Compare January 22, 2018 05:08
@matthewleon
Copy link
Contributor Author

ready for review

@hdgarrood
Copy link
Contributor

This is great, but I would like to preserve the property that nub xs == xs whenever xs has no duplicates. Perhaps it’s not too difficult to modify this approach so that this property is preserved, by doing something like mapWithIndex (flip Tuple) before sorting, and sorting by snd again at the end?

@hdgarrood
Copy link
Contributor

Also if we don’t have a test for that property, I’d quite like to add one. Doesn’t have to be via quickcheck; if dependencies make quickcheck difficult that’s fine, we can just test it on some hardcoded set of cases.

@hdgarrood
Copy link
Contributor

Could you reference the original issue in the commit as well please?

@garyb
Copy link
Member

garyb commented Jan 22, 2018

Very much agreed w/preserving the original order!

@matthewleon
Copy link
Contributor Author

Will use this if merged: #130

addresses purescript#91

O(n*logn)
@matthewleon
Copy link
Contributor Author

I've incorporated the suggestions above. This is ready for review.

@matthewleon
Copy link
Contributor Author

Actually, I'll add nubEq on this one as well. Why not?

@matthewleon
Copy link
Contributor Author

Okay, ready for review :).

Copy link
Contributor

@hdgarrood hdgarrood left a comment

Choose a reason for hiding this comment

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

I just realised, if nub has an Ord constraint, then nubBy should really take an a -> a -> Ordering, so that nub = nubBy compare. We could rename the current nubBy to nubEqBy.

The old `nubBy` becomes `nubByEq`, which only requires an equality
relation.
@matthewleon
Copy link
Contributor Author

nubBy should really take an a -> a -> Ordering, so that nub = nubBy compare. We could rename the current nubBy to nubEqBy.

Done.

@hdgarrood
Copy link
Contributor

Looks great! Do you know what the big-O for this new implementation should be?

Since this is breaking I think it makes sense to merge this as part of the 0.12 updates.

@matthewleon
Copy link
Contributor Author

matthewleon commented Jan 28, 2018

Do you know what the big-O for this new implementation should be?

Your upper bound should be set by sort, which uses FFI. For the common case in which you're on a major JS VM, this is O(nlogn).

Hopefully my thinking here is correct. Should I add a comment to this effect?

@matthewleon
Copy link
Contributor Author

Do you know what the big-O for this new implementation should be?

I should add that if for some reason your FFI'd STArray.push has some kind of pathological complexity, that would of course bring you potentially above O(nlogn). But that seems like a really fringe case.

@hdgarrood
Copy link
Contributor

Sounds good. I think we can assume that push is amortized O(1) (even though 5 minutes of googling was kind of inconclusive).

@garyb garyb changed the base branch from master to compiler/0.12 May 13, 2018 16:31
@kritzcreek
Copy link
Member

I merged this by hand into the compiler/0.12 branch. Thanks!

@kritzcreek kritzcreek closed this May 15, 2018
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.

4 participants