-
Notifications
You must be signed in to change notification settings - Fork 17
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
Support the creation of Sets from Maps #21
Comments
Could we get a bit more motivation for this addition? What use-case isn't covered by |
most of the |
immutable.HashMap uses structural sharing for inserts as well as removals https://github.com/scala/scala/blob/2.13.x/src/library/scala/collection/immutable/HashMap.scala#L70 Maybe we can port those optimizations to immutable.Map and the other abstract Map subclasses (SeqMap, SortedMap) without having to introduce new collection types |
what about mutable |
Also, I don't think it's a reasonable burden to put on Two of the main motivators for this are to provide implementations for the following types (that do not currently exist) based on currently existing
|
I don't think there need be any burden put on Map implementers. In // Map.scala
self =>
override def keySet: Set[K] = new ImmutableKeySet
/** The implementation class of the set returned by `keySet` */
protected class ImmutableKeySet extends AbstractSet[K] with GenKeySet with DefaultSerializable {
def incl(elem: K): Set[K] = if (this(elem)) this else (self + (elem -> null)).keySet
def excl(elem: K): Set[K] = if (this(elem)) (self - elem).keySet else this
} Then in // SortedMap.scala
self =>
override def keySet: SortedSet[K] = new ImmutableKeySortedSet
/** The implementation class of the set returned by `keySet` */
protected class ImmutableKeySortedSet extends AbstractSet[K] with SortedSet[K] with GenKeySet with GenKeySortedSet {
def rangeImpl(from: Option[K], until: Option[K]): SortedSet[K] = {
val map = self.rangeImpl(from, until)
new map.ImmutableKeySortedSet
}
def incl(elem: K): SortedSet[K] = if (this(elem)) this else (self + (elem -> null)).keySet
def excl(elem: K): SortedSet[K] = if (this(elem)) (self - elem).keySet else this
} SeqMap already gets the optimization inherited from Map, and then hypothetically if we ever added // SeqMap.scala
self =>
override def keySet: SeqSet[K] = new ImmutableKeySeqSet
protected class ImmutableKeySeqSet extends AbstractSet[K] with SortedSet[K] with GenKeySet with GenKeySeqSet {
def incl(elem: K): SeqSet[K] = if (this(elem)) this else (self + (elem -> null)).keySet
def excl(elem: K): SeqSet[K] = if (this(elem)) (self - elem).keySet else this
} Any new implementation of Map, SeqMap or SortedMap with get the optimization passed down and wouldn't need to define it again unless they wanted to add their own special semantics around the keySet |
There are still several things you're missing:
|
I'm not seeing the practical use-case of a SetFromMap whose |
the same use case as all other collections that return the same implementation for transformations?
apparently common enough for Java to have it (
the implementation would be private; just the functionality would be public |
@NthPortal I must say that I’m not really convinced by the use case. Maybe I misunderstood. Could we improve the implementation of |
I plan to reply to this, but I don't have the capacity right now |
sorry it's taken me so long to reply. There are basically two questions:
I opine that the answer to (1) is "yes". For a variety of reasons, I do not believe
Additionally, I just don't think that I also don't think all that said, I'm happy to move my implementation to collection-contrib if that's deemed a more appropriate place for it |
Add implementations for
Set
s backed byMap
s (and corresponding factories).Add fluent
fromMap
APIs toSet
companions.Motivation:
The text was updated successfully, but these errors were encountered: