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

Make list.unique logarithmic instead of quadratic #678

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

radekm
Copy link

@radekm radekm commented Aug 14, 2024

Fixes #667. New list.unique implementation uses gleam/set to detect whether item has already been seen. New list.unique implementation uses gleam/dict to detect whether item has already been seen (it can't use gleam/set because that would create import cycle).

Copy link
Member

@lpil lpil left a comment

Choose a reason for hiding this comment

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

Thank you 💜

@radekm
Copy link
Author

radekm commented Aug 14, 2024

But unfortunately there's an import cycle.

The import statements for these modules form a cycle:

    ┌─────┐
    │     gleam/result
    │     ↓
    │     gleam/set
    │     ↓
    │     gleam/list
    └─────┘

@lpil
Copy link
Member

lpil commented Aug 14, 2024

Looks like you have a compile error!

@lpil lpil marked this pull request as draft August 14, 2024 09:36
@radekm
Copy link
Author

radekm commented Aug 14, 2024

So I have to come up with a different solution

@radekm radekm force-pushed the improve-perf-of-list-unique branch from 236a899 to bd0da5a Compare August 14, 2024 10:10
@radekm radekm marked this pull request as ready for review August 14, 2024 10:16
@@ -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.
Copy link
Member

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

@giacomocavalieri
Copy link
Member

giacomocavalieri commented Nov 12, 2024

This looks good to me! I've left a small note inline. Also could you update the changelog?

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.

list.unique needs quadratic time
3 participants