Skip to content

Commit 8d33df1

Browse files
start groupBy examples
1 parent edb1e60 commit 8d33df1

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package rx.lang.scala.examples
2+
3+
object Olympics {
4+
case class Medal(val year: Int, val games: String, val discipline: String, val medal: String, val athlete: String, val country: String)
5+
6+
val mountainBikeMedals = List(
7+
Medal(2012, "London 2012", "cross-country men", "Gold", "Jaroslav KULHAVY", "Czech Republic"),
8+
Medal(2012, "London 2012", "cross-country men", "Silver", "Nino SCHURTER", "Switzerland"),
9+
Medal(2012, "London 2012", "cross-country men", "Bronze", "Marco Aurelio FONTANA", "Italy"),
10+
Medal(2012, "London 2012", "cross-country women", "Gold", "Julie BRESSET", "France"),
11+
Medal(2012, "London 2012", "cross-country women", "Silver", "Sabine SPITZ", "Germany"),
12+
Medal(2012, "London 2012", "cross-country women", "Bronze", "Georgia GOULD", "United States of America"),
13+
Medal(2008, "Beijing 2008", "cross-country women", "Gold", "Sabine SPITZ", "Germany"),
14+
Medal(2008, "Beijing 2008", "cross-country women", "Silver", "Maja WLOSZCZOWSKA", "Poland"),
15+
Medal(2008, "Beijing 2008", "cross-country women", "Bronze", "Irina KALENTYEVA", "Russian Federation"),
16+
Medal(2008, "Beijing 2008", "cross-country men", "Gold", "Julien ABSALON", "France"),
17+
Medal(2008, "Beijing 2008", "cross-country men", "Silver", "Jean-Christophe PERAUD", "France"),
18+
Medal(2008, "Beijing 2008", "cross-country men", "Bronze", "Nino SCHURTER", "Switzerland"),
19+
Medal(2004, "Athens 2004", "cross-country men", "Gold", "Julien ABSALON", "France"),
20+
Medal(2004, "Athens 2004", "cross-country men", "Silver", "Jose Antonio HERMIDA RAMOS", "Spain"),
21+
Medal(2004, "Athens 2004", "cross-country men", "Bronze", "Bart BRENTJENS", "Netherlands"),
22+
Medal(2004, "Athens 2004", "cross-country women", "Gold", "Gunn-Rita DAHLE", "Norway"),
23+
Medal(2004, "Athens 2004", "cross-country women", "Silver", "Marie-Helene PREMONT", "Canada"),
24+
Medal(2004, "Athens 2004", "cross-country women", "Bronze", "Sabine SPITZ", "Germany"),
25+
Medal(2000, "Sydney 2000", "cross-country women", "Gold", "Paola PEZZO", "Italy"),
26+
Medal(2000, "Sydney 2000", "cross-country women", "Silver", "Barbara BLATTER", "Switzerland"),
27+
Medal(2000, "Sydney 2000", "cross-country women", "Bronze", "Marga FULLANA", "Spain"),
28+
Medal(2000, "Sydney 2000", "cross-country men", "Gold", "Miguel MARTINEZ", "France"),
29+
Medal(2000, "Sydney 2000", "cross-country men", "Silver", "Filip MEIRHAEGHE", "Belgium"),
30+
Medal(2000, "Sydney 2000", "cross-country men", "Bronze", "Christoph SAUSER", "Switzerland"),
31+
Medal(1996, "Atlanta 1996", "cross-country men", "Silver", "Thomas FRISCHKNECHT", "Switzerland"),
32+
Medal(1996, "Atlanta 1996", "cross-country men", "Bronze", "Miguel MARTINEZ", "France"),
33+
Medal(1996, "Atlanta 1996", "cross-country men", "Gold", "Bart BRENTJENS", "Netherlands"),
34+
Medal(1996, "Atlanta 1996", "cross-country women", "Gold", "Paola PEZZO", "Italy"),
35+
Medal(1996, "Atlanta 1996", "cross-country women", "Silver", "Alison SYDOR", "Canada"),
36+
Medal(1996, "Atlanta 1996", "cross-country women", "Bronze", "Susan DEMATTEI", "United States of America")
37+
).reverse
38+
39+
}

language-adaptors/rxjava-scala/src/main/scala/rx/lang/scala/examples/RxScalaDemo.scala

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import org.junit.{Before, Test, Ignore}
2323
import org.junit.Assert._
2424
import rx.lang.scala.concurrency.NewThreadScheduler
2525

26-
@Ignore // Since this doesn't do automatic testing, don't increase build time unnecessarily
26+
//@Ignore // Since this doesn't do automatic testing, don't increase build time unnecessarily
2727
class RxScalaDemo extends JUnitSuite {
2828

2929
@Test def intervalExample() {
@@ -158,6 +158,49 @@ class RxScalaDemo extends JUnitSuite {
158158
waitFor(o)
159159
}
160160

161+
def sampleAllUntilComplete[T](o: Observable[T], period: Duration): Observable[T] = {
162+
for ((element, tick) <- o zip Observable.interval(period)) yield element
163+
}
164+
165+
@Ignore //TODO
166+
@Test def groupByExample() {
167+
import Olympics._
168+
// `: _*` converts list to varargs
169+
val medals = Observable[Medal](Olympics.mountainBikeMedals : _*)
170+
171+
// 1 second = 4 years :D
172+
val medalsByYear = sampleAllUntilComplete(medals.groupBy(_.year), 1 seconds)
173+
174+
/*
175+
val t = (for ((year, medals) <- medalsByYear) yield medals).flatMap(ms => ms)
176+
t.subscribe(println(_))
177+
*/
178+
179+
val timedMedals = for ((year, medals) <- medalsByYear; medal <- medals) yield medal
180+
181+
timedMedals.subscribe(println(_)) // doesn't print ???
182+
183+
Thread.sleep(5000)
184+
185+
/*
186+
medalsByYear.subscribe(p => println(p._1))
187+
188+
//waitFor(medalsByYear)
189+
190+
val byCountry = medals.groupBy(_.country)
191+
192+
def score(medals: Observable[Medal]) = medals.fold((0, 0, 0))((s, m) => (s, m.medal) match {
193+
case ((gold, silver, bronze), "Gold") => (gold+1, silver, bronze)
194+
case ((gold, silver, bronze), "Silver") => (gold, silver+1, bronze)
195+
case ((gold, silver, bronze), "Bronze") => (gold, silver, bronze+1)
196+
})
197+
198+
val scores = for ((country, medals) <- byCountry) yield (country, score(medals))
199+
200+
println(scores.toBlockingObservable.toList)
201+
*/
202+
}
203+
161204
def output(s: String): Unit = println(s)
162205

163206
// blocks until obs has completed

0 commit comments

Comments
 (0)