Skip to content

Commit edb1e60

Browse files
first Scala groupBy implementation
1 parent 141bb6b commit edb1e60

File tree

1 file changed

+13
-39
lines changed

1 file changed

+13
-39
lines changed

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/Observable.scala

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,51 +1215,25 @@ class Observable[+T](val asJava: rx.Observable[_ <: T])
12151215
// because we can just use ++ instead
12161216

12171217
/**
1218-
* Groups the items emitted by an Observable according to a specified criterion, and emits these
1219-
* grouped items as {@link GroupedObservable}s, one GroupedObservable per group.
1220-
* <p>
1221-
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/groupBy.png">
1218+
* Groups the items emitted by this Observable according to a specified discriminator function.
12221219
*
1223-
* @param keySelector
1220+
* @param f
12241221
* a function that extracts the key from an item
1225-
* @param elementSelector
1226-
* a function to map a source item to an item in a {@link GroupedObservable}
1227-
* @param <K>
1228-
* the key type
1229-
* @param <R>
1230-
* the type of items emitted by the resulting {@link GroupedObservable}s
1231-
* @return an Observable that emits {@link GroupedObservable}s, each of which corresponds to a
1232-
* unique key value and emits items representing items from the source Observable that
1233-
* share that key value
1234-
*/
1235-
/* TODO make a Scala GroupedObservable and groupBy
1236-
def groupBy[K,R](keySelector: T => K, elementSelector: T => R ): Observable[GroupedObservable[K,R]] = {
1237-
???
1238-
}
1239-
*/
1240-
// public <K, R> Observable<GroupedObservable<K, R>> groupBy(final Func1<? super T, ? extends K> keySelector, final Func1<? super T, ? extends R> elementSelector)
1241-
1242-
/**
1243-
* Groups the items emitted by an Observable according to a specified criterion, and emits these
1244-
* grouped items as {@link GroupedObservable}s, one GroupedObservable per group.
1245-
* <p>
1246-
* <img width="640" src="https://github.com/Netflix/RxJava/wiki/images/rx-operators/groupBy.png">
1247-
*
1248-
* @param keySelector
1249-
* a function that extracts the key for each item
12501222
* @param <K>
1251-
* the key type
1252-
* @return an Observable that emits {@link GroupedObservable}s, each of which corresponds to a
1253-
* unique key value and emits items representing items from the source Observable that
1254-
* share that key value
1223+
* the type of keys returned by the discriminator function.
1224+
* @return an Observable that emits {@code (key, observable)} pairs, where {@code observable}
1225+
* contains all items for which {@code f} returned {@code key}.
12551226
*/
1256-
/* TODO
1257-
def groupBy[K](keySelector: T => K ): Observable[GroupedObservable[K,T]] = {
1258-
???
1227+
def groupBy[K](f: T => K): Observable[(K, Observable[T])] = {
1228+
val o1 = asJava.groupBy[K](f) : rx.Observable[_ <: rx.observables.GroupedObservable[K, _ <: T]]
1229+
val func = (o: rx.observables.GroupedObservable[K, _ <: T]) => (o.getKey(), Observable[T](o))
1230+
Observable[(K, Observable[T])](o1.map[(K, Observable[T])](func))
12591231
}
1260-
*/
1261-
// public <K> Observable<GroupedObservable<K, T>> groupBy(final Func1<? super T, ? extends K> keySelector)
12621232

1233+
// There's no method corresponding to
1234+
// public <K, R> Observable<GroupedObservable<K, R>> groupBy(final Func1<? super T, ? extends K> keySelector, final Func1<? super T, ? extends R> elementSelector)
1235+
// because this can be obtained by combining groupBy and map (as in Scala)
1236+
12631237
/**
12641238
* Given an Observable that emits Observables, creates a single Observable that
12651239
* emits the items emitted by the most recently published of those Observables.

0 commit comments

Comments
 (0)