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

Adding additional set builtins #506

Merged
merged 8 commits into from
Sep 12, 2023

Conversation

SamWheating
Copy link
Contributor

This PR enables the following operations:

  • S.difference(x) AKA S - x
  • S.intersection(x) AKA S & x
  • S.symmetric_difference(x) AKA S ^ x
  • S.issubset(x) AKA S <= x or s < x
  • S.issuperset(x) AKA S >= x or S > x

I also unified the unified the implementations of these operations which were inlined in the evaluator with the implementation defined in values.go, to reduce duplication.

Demo:

Welcome to Starlark (go.starlark.net)
>>> x = set([1,2,3])
>>> y = set([3,4,5])
>>> x.intersection(y)
set([3])
>>> x.difference(y)
set([1, 2])
>>> x.symmetric_difference(y)
set([1, 2, 4, 5])
>>> x.issuperset(y)
False
>>> x.issuperset(set([1,2]))
True
>>> x.issubset(y)
False
>>> x.issubset(set([1,2,3,4]))
True

Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

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

Very nice! Thanks for contributing this feature, with docs and tests.

doc/spec.md Outdated Show resolved Hide resolved
doc/spec.md Outdated Show resolved Hide resolved
doc/spec.md Outdated Show resolved Hide resolved
doc/spec.md Outdated Show resolved Hide resolved
doc/spec.md Outdated Show resolved Hide resolved
starlark/value.go Outdated Show resolved Hide resolved
starlark/value.go Outdated Show resolved Hide resolved
starlark/value.go Outdated Show resolved Hide resolved
starlark/value.go Outdated Show resolved Hide resolved
starlark/value.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

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

Looks great, thanks again.

<a id='set·difference'></a>
### set·difference

`S.difference(y)` returns a new set into which have been inserted all the elements of set S which are not in y.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This phrasing nicely finesses both the question of content and of ordering.

@adonovan adonovan merged commit 745481c into google:master Sep 12, 2023
7 checks passed
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