pull 'Set'-producing operations | & ~ up to 'Collection'
See original GitHub issueI was longing for Set
operators/operations in Range
instances. Namely union
, intersection
but why not also complement
and exclusiveUnion
.
Sure, I can do set(3..5).union(set(4..6))
, but is there any reason that Range
couldn’t satisfy Set
? I read through #3557 and the current interfaces and their implementations and although I didn’t perhaps catch everything, I felt I should ask.
Since all operations except intersection
may result in split ranges, the implied return value of Set
is actually just fine. One could of course have a more specific Range<Element> intersectionWithRange(Range<Element> other)
for convenience.
For these set operations one would naturally not care if the ranges are increasing
or decreasing
- the result would be the same i.e. the result would be normalized.
As for an actual implementation it would perhaps be beneficial to override the default implementations from the Set
interface for more efficient operation (as long as the “other” set is of same type as “this” set), but they would still be “just Sets”…
Thanks for listening…
Issue Analytics
- State:
- Created 5 years ago
- Comments:32 (26 by maintainers)
Top GitHub Comments
I’ve implemented this idea (it was really straightforward) and it seems to be working well. I’ll push it to a branch later.
One possible reasonable thing to do would be to define
|
and&
for theCollection
interface, but have them always produceSet
s. That is, simply pull the currentunion()
andintersection()
methods up toCollection
fromSet
.That would be a change with pretty minimal impact on the language, that makes the existing
|
and&
operators much more useful.For example,
[1->"hello", 3->"world"] | map { 1->"goodbye", 2->"sweet" }
would be equal toset { 1->"hello", 3->"world", 1->"goodbye", 2->"sweet" }
.