-
Notifications
You must be signed in to change notification settings - Fork 111
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
Refactor toMultimap #68
Conversation
// It's complicated to convert `mutable.Map[K, mutable.Buffer[V]]` to `java.util.Map[K, java.util.Collection[V]]`, | ||
// so RxScala implements `toMultimap` directly. | ||
// Choosing `mutable.Buffer/Map` is because `append/update` is necessary to implement an efficient `toMultimap`. | ||
def toMultiMap[K, V, M <: mutable.MultiMap[K, V]](keySelector: T => K, valueSelector: T => V, multiMapFactory: () => M): Observable[M] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- I think a by name parameter (i.e.
multiMapFactory: => M
) would be more scala-ish thanmultiMapFactory: () => M
. - Can you add default arguments for
multiMapFactory
and maybe even forvalueSelector
? Then you could remove some overloads and get a smaller API.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default value for multiMapFactory
does not work. If I change it to
def toMultiMap[K, V, M <: mutable.MultiMap[K, V]](
keySelector: T => K,
valueSelector: T => V,
multiMapFactory: => M = new mutable.HashMap[K, mutable.Set[V]] with mutable.MultiMap[K, V]): Observable[M] = {
There will be some error such as
RxScala/src/test/scala/rx/lang/scala/ObservableTest.scala:242: type mismatch;
[error] found : String => String
[error] required: String => V
[error] val o = Observable.just("a", "b", "cc", "dd").toMultiMap(_.length, s => s + s)
Looks the compiler can not infer the type V
from _.length
, s => s + s
and the default value new mutable.HashMap[String, mutable.Set[V]] with mutable.MultiMap[String, V]
.
When I saw the following signature, toMultiMap[K, V, M <: mutable.MultiMap[K, V]](keySelector: T => K, valueSelector: T => V, multiMapFactory: => M) I thought it might be better to use currying for toMultiMap[K, V, M <: mutable.MultiMap[K, V]](multiMapFactory: => M)(keySelector: T => K, valueSelector: T => V) It does not work because
toMultiMap[K, V, M <: mutable.MultiMap[K, V]](keySelector: T => K, valueSelector: T => V)(multiMapFactory: => M) It also does not work because
At last, looks the following signature is best now: toMultiMap[K, V, M <: mutable.MultiMap[K, V]](keySelector: T => K, valueSelector: T => V, multiMapFactory: => M) |
@samuelgruetter further suggestion? |
LGTM. |
@zsxwing could you please write some release notes giving a summary about the changes of |
Conflicts: src/test/scala/rx/lang/scala/CompletenessTest.scala
OK. |
This PR includes the following breaking changes:
Observable[scala.collection.Map[K, Seq[T]]]
toObservable[mutable.MultiMap[K, V]]
.toMultimap
totoMultiMap
to make it consistent to the return type.toMultimap(keySelector, valueSelector, mapFactory, bufferFactory)
. You can overrideMultiMap.makeSet
to create your custom bufferFactory. E.g.,