-
Notifications
You must be signed in to change notification settings - Fork 21
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
Map's filterKeys and mapValues #4776
Comments
Imported From: https://issues.scala-lang.org/browse/SI-4776?orig=1 |
Lanny Ripple (lanny ripple) said: |
Aaron Novstrup (anovstrup) said: |
@dcsobral said: |
Nicholas Sterling (amigonico) said: |
Alexey Ermakov (technocoreai) said: |
@rklaehn said (edited on Jan 23, 2014 2:29:36 PM UTC): In the new scala.collection.mutable.LongMap/AnyRefMap, @Ichoran added a method mapValuesNow . Maybe we should just add mapValuesNow and filterKeysNow to the map interface and implement it in the other collections? For immutable.HashMap, mapValuesNow would be easy to implement and very efficient. |
Ben Challenor (bchallenor) said: |
@Blaisorblade said:
One could still change the return type to be more specific, like MapView (which somebody should write) or sth. like FilterMonadic — that would add a force method, and this would help for instance for #7005.
That's something which is possible in addition, and much easier because it wouldn't require implementing MapView. It takes more time to write tests than actually making this right: Map[K, A] ... {
def mapValuesNow(f: A => B): Map[K, B] = map { case (a, b) => (a, f(b)) }
} Or Rüdiger, are you thinking of something more efficient? |
@rklaehn said: For scala.collection.immutable.HashMap it would be possible (and pretty trivial) to write a mapValuesNow that is very high performance. You can basically keep the entire Trie data structure and just transform the values of the HashMap1 and HashMapCollision1 leaf nodes. So you would e.g. not have to call hashCode or equals at all, whereas the generic mapValuesNow method calls them a lot and rebuilds the entire Trie from scratch. filterKeysNow is a bit more complex, but you could probably reuse some of the filter code of HashSet. For the high performance mutable HashMap from Rex (AnyRefMap and LongMap), mapValuesNow is already implemented. Regarding the immutable Map1..Map4: I don't really like them either since they complicate writing high performance tree/tree operations. Writing mapValuesNow for Map1..Map4 would be trivial, but a lot of boilerplate. |
@dragos said: In an ideal world We could:
The second choice sounds the most reasonable to me. Thoughts? |
@dwijnand said (edited on Dec 18, 2015 4:27:58 PM UTC): |
@Blaisorblade said: As an advantage, if somebody volunteers, I expect one could provide, in a separate library, the same new methods for 2.11 through extension methods (at the cost of one extra import I'm afraid). By offering the same package for 2.12 (though empty), people interested in Java 6/7 could use the new methods both on 2.11 and on 2.12. Following the existing roadmap (http://www.scala-lang.org/news/roadmap-next/), in Scala Aida we could then remove the existing |
@dragos said: |
Omer van Kloeten (omervk) said:
This would keep the library standardized, supplying only strict implementations, allowing users to explicitly use non-strict versions if they see fit. |
@Blaisorblade said: I considered suggesting such a variant of 2, but I sense current views are often considered unsatisfactory — so I'm not sure I'd propose extending them. Finally, for such a proposal somebody should volunteer to write MapView by 2.12 — I don't want to stall this proposal. |
@Ichoran said: |
@rklaehn said: Regarding the implementation: eager mapValues for s.c.i.HashMap can be implemented very efficiently, same for s.c.i.SortedMap. Eager filterKeys for HashMap is also possible to implement quickly. |
@dragos said: |
@Blaisorblade said: But @dragos is right if the overall goal of the bug is not "offer both behaviors at all" but "fix latent bugs where clients use the lazy variant but shouldn't". Under that goal, deprecation is essential to the overall goal. I think that's a good goal, since Scala tries to reduce pitfalls. The only downside is that some users will have to change their code just to confirm they mean to use the lazy version. |
in the new collections (which will ship in 2.13.0-M4), both |
I've just spent three frustrating hours debugging, trying to understand why the heck values are evaluated out of order, only to eventually find out this issue. Ugh. |
@SethTisue @julienrf @szeiger - I think we should deprecate Alternatively, leave it alone but return a value class with a single method, |
Submitted #10919 |
In revision 21018,
filterKeys
andmapValues
started returning map views in everything but name. This cause a few problems, however -- the lack of a distinct type does not help identifying them as returning non-strict collections, and they lack aforce
method to retrieve a strict collection.I'd suggest returning a
MapView
, but there isn't any such collection at the moment --view
returns anIterableView
instead. Perhaps the ideal would be to create aMapView
, and makefilterKeys
andmapValues
only non-strict in them.The text was updated successfully, but these errors were encountered: