Skip to content

Commit

Permalink
Add a Starlark set data type (#290)
Browse files Browse the repository at this point in the history
Modeled on the Python 3 set type and the existing implementations in Go, Rust, and Java, with the following differences:

* set literals and set comprehensions *not* supported (unlike python3)
  * Rationale: readability; `{}` can be confusing (empty set or empty dict?)
* `copy()` method *not* supported (unlike python3)
  * Rationale: we do not have it on lists or dictionaries; if we add it, we ought to add it to all containers for symmetry.
* comparison operators *not* supported (unlike starlark-go and python3)
  * Rationale: readability; python3-style comparison operators on sets provide only a partial order, resulting in unexpected behavior if one, for example, attempts to sort a list of sets.
* `update()` method supported (unlike starlark-go)
  * Rationale: follow python's example, useful for in-place mutation when rhs is not a set
* `isdisjoint()`, `intersection_update()`, `difference_update()`, `symmetric_difference_update()` method supported (unlike starlark-go and starlark-rust)
  * Rationale: follow python's example for a tiny efficiency gain - e.g. if we didn't have `s.intersection_update(rhs)`, and `rhs` was a non-set sequence, we'd need to instead do `s &= set(rhs)`, which would mean allocating an unnecessary temporary set for rhs's elements.
* multiple-argument form of `union()`, `intersection()`, `difference()` and corresponding _update methods supported (unlike starlark-go and starlark-rust)
  * Rationale: follow python's example for a tiny efficiency gain for the non-_update methods - allows avoiding temporary intermediate sets when unioning (or intersecting, etc.) a collection of sequences/sets into a set. (For the _update methods, the multi-argument form is syntactic sugar and doesn't provide an efficiency gain, but seems useful for api symmetry.)
* `|`, `&`, `-`, and `^` operators (and their augmented forms) require *both* sides to be sets if lhs is a set (unlike starlark-go)
  * Rationale: preserve syntactic compatibility with python3; starlark's `|` operator already requires both sides to be dicts if lhs is a dict.

Fixes #264
  • Loading branch information
tetromino authored Dec 17, 2024
1 parent 931e5c8 commit b130d3b
Showing 1 changed file with 457 additions and 5 deletions.
Loading

0 comments on commit b130d3b

Please sign in to comment.