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

feat: add Set stdlib module #466

Merged
merged 1 commit into from
Dec 17, 2020
Merged

feat: add Set stdlib module #466

merged 1 commit into from
Dec 17, 2020

Conversation

zweimach
Copy link
Contributor

@zweimach zweimach commented Dec 12, 2020

Add initial implementation of Set module using List as the the same underlying data structure as Map.
It reuses many modified helper functions from the Map module.
Maybe we could unify those functions?

@ospencer, what is the convention for init and make family of functions?

Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

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

This is awesome! Thanks for putting this together 😄

Re: make vs. init, make creates an empty structure, and init allows you to create a structure with some initial data. Hopefully that answers your question, but let me know if that's not what you were asking.

I think this largely looks good, but for the backing data structure, I believe that we do want it to be an Array, like Maps. Sets are essentially just Map<t, Void>, so the implementation can be pretty much exactly the same as Map, but the buckets would not have a value property. This way, your Set.contains and Set.add are both O(1) in the average case, versus O(n) to search/insert into a list.

Let me know if that makes sense or if there's anything I can clarify!

Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

This is AWESOME! A few questions/changes that might be able to improve it.

compiler/test/stdlib/set.test.gr Show resolved Hide resolved
compiler/test/stdlib/set.test.gr Show resolved Hide resolved
stdlib/set.gr Outdated Show resolved Hide resolved
stdlib/set.gr Outdated Show resolved Hide resolved
stdlib/set.gr Outdated
let set = make();
List.forEach((key) => {
add(key, set);
}, List.unique(list));
Copy link
Member

Choose a reason for hiding this comment

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

We should be able to drop the unique call here, since add should take care of it (and it'll be faster!)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, add should be enough. Thank you.

stdlib/set.gr Outdated
let set = make();
Array.forEach((key) => {
add(key, set);
}, Array.unique(array));
Copy link
Member

Choose a reason for hiding this comment

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

And here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you.

@ospencer ospencer linked an issue Dec 17, 2020 that may be closed by this pull request
Copy link
Member

@phated phated left a comment

Choose a reason for hiding this comment

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

This looks great! Awesome work @zweimach!!

Copy link
Member

@ospencer ospencer left a comment

Choose a reason for hiding this comment

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

Fantastic work! So excited to have this done! 🎉

@ospencer ospencer merged commit 846088f into grain-lang:master Dec 17, 2020
@zweimach zweimach deleted the set branch December 18, 2020 11:09
ospencer pushed a commit that referenced this pull request Jan 11, 2021
ospencer pushed a commit that referenced this pull request Jan 16, 2021
ospencer pushed a commit that referenced this pull request Jan 16, 2021
ospencer pushed a commit that referenced this pull request Jan 16, 2021
ospencer pushed a commit that referenced this pull request Jan 16, 2021
@github-actions github-actions bot mentioned this pull request May 31, 2022
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.

Stdlib: Add Set module
3 participants