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

FR: Support More Methods for type set #492

Open
ohmyx opened this issue Jul 26, 2023 · 7 comments
Open

FR: Support More Methods for type set #492

ohmyx opened this issue Jul 26, 2023 · 7 comments
Assignees

Comments

@ohmyx
Copy link

ohmyx commented Jul 26, 2023

As per PYthon doc : https://docs.python.org/3.10/library/stdtypes.html#set-types-set-frozenset

The five methods should be added:

add(elem)
Add element elem to the set.

remove(elem)
Remove element elem from the set. Raises KeyError if elem is not contained in the set.

discard(elem)
Remove element elem from the set if it is present.

pop()
Remove and return an arbitrary element from the set. Raises KeyError if the set is empty.

clear()
Remove all elements from the set.

Now it's too hard to use, for example, add() can only be implemented via helper like:

def set_add(s, x):
    l = list(s)
    l.append(x)
    return set(l)
@qms95
Copy link

qms95 commented Jul 30, 2023

+1
当前set太鸡肋了!

@adonovan
Copy link
Collaborator

adonovan commented Aug 4, 2023

These operations are all compatible with Python and consistent with starlark-go's notion of set, and they are useful but awkward or inefficient to implement in Starlark itself, so I would accept a PR to add all of these methods. (set is a starlark-go-specific extension, so there's no need for a proposal.)

@SamWheating
Copy link
Contributor

SamWheating commented Aug 14, 2023

I'm looking for something to work on, so I've started implementing this. Feel free to assign this issue to me and I'll try to have a PR open soon 👍

@nihaobad
Copy link

@SamWheating Thanks for your work!

Can you please add the following useful methods as well?

union()        -> a | b
intersection() -> a & b
difference()   -> a - b
symmetric_difference()  -> a ^ b

issubset()     -> a <= b
issuperset()   -> a >= b

@adonovan
Copy link
Collaborator

The |, &, ^ operators on sets are already supported, but the-, <=, and >= operators are missing. The union method is present but the others are missing. All seem like reasonable features to add.

@ohmyx
Copy link
Author

ohmyx commented Aug 30, 2023

The |, &, ^ operators on sets are already supported, but the-, <=, and >= operators are missing. The union method is present but the others are missing. All seem like reasonable features to add.

Yes. It would be nice to have those methods, 'cause the symbols are initially elusive in understanding. @adonovan

Welcome to Starlark (go.starlark.net)
>>> x = set([1,2,3])
>>> y = set([3,5,6])
>>> x & y
set([3])
>>> x | y
set([1, 2, 3, 5, 6])
>>> x - y
Traceback (most recent call last):
  <stdin>:1:3: in <expr>
Error: unknown binary op: set - set
>>> x ^ y
set([1, 2, 5, 6])
>>> x <= y
Traceback (most recent call last):
  <stdin>:1:3: in <expr>
Error: set <= set not implemented
>>> x >= y
Traceback (most recent call last):
  <stdin>:1:3: in <expr>
Error: set >= set not implemented

@SamWheating
Copy link
Contributor

Good idea, I will add those functions as well as the infix operators in the next few days.

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

No branches or pull requests

5 participants