-
-
Notifications
You must be signed in to change notification settings - Fork 182
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
Make list.unique
logarithmic instead of quadratic
#678
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you 💜
But unfortunately there's an import cycle.
|
Looks like you have a compile error! |
So I have to come up with a different solution |
236a899
to
bd0da5a
Compare
src/gleam/list.gleam
Outdated
@@ -1151,7 +1152,7 @@ pub fn intersperse(list: List(a), with elem: a) -> List(a) { | |||
|
|||
/// Removes any duplicate elements from a given list. | |||
/// | |||
/// This function returns in loglinear time. | |||
/// This function returns in logarithmic time. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is wrong, the algorithm is loglinear O(n logn)
since it still has to iterate over the whole list
This looks good to me! I've left a small note inline. Also could you update the changelog? |
59a5293
to
c6a00f6
Compare
Thanks for the review. I have fixed the comment and added an entry to the changelog. |
Minor suggestion: consider using a Set rather than a Dict. I know Set uses Dict under the covers, but conceptually you are using your Dict as a Set, so just use a Set. |
It can't use a |
Ah, thanks for the clarification. Perhaps adding a comment to the code about that point would prevent future readers from making the same comment/question. |
Yeah good idea! |
c6a00f6
to
b9098ca
Compare
Thanks, comment added |
Thank you. How does this version benchmark compared to the previous one? It would be fab to see what this tool returns for lists of different sizes. |
Are you still working on this @radekm ? Thank you |
@lpil I did the benchmarking using glychee and these are the results on a list of growing size of unique elements (the worst case for the algorithm as all elements are unique):
This is a huge improvement!
pub fn unique(list: List(a)) -> List(a) {
unique_loop(list, dict.new(), [])
}
fn unique_loop(list: List(a), seen: Dict(a, Nil), acc: List(a)) -> List(a) {
case list {
[] -> reverse(acc)
[first, ..rest] ->
case dict.has_key(seen, first) {
True -> unique_loop(rest, seen, acc)
False ->
unique_loop(rest, dict.insert(seen, first, Nil), [first, ..acc])
}
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you! Let's use the manually recursive version. @radekm would you like to add that?
Fixes #667.
NewNewlist.unique
implementation usesgleam/set
to detect whether item has already been seen.list.unique
implementation usesgleam/dict
to detect whether item has already been seen (it can't usegleam/set
because that would create import cycle).