@@ -187,10 +187,19 @@ class RxScalaDemo extends JUnitSuite {
187
187
o.buffer(5 ).subscribe((l : Seq [Int ]) => println(l.mkString(" [" , " , " , " ]" )))
188
188
}
189
189
190
- @ Test def bufferExample () {
190
+ @ Test def tumblingBufferExample () {
191
191
val o = Observable .from(1 to 18 ).zip(Observable .interval(100 millis)).map(_._1)
192
192
val boundary = Observable .interval(500 millis)
193
- o.buffer(boundary).toBlocking.foreach((l : Seq [Int ]) => println(l.mkString(" [" , " , " , " ]" )))
193
+ o.tumblingBuffer(boundary).toBlocking.foreach((l : Seq [Int ]) => println(l.mkString(" [" , " , " , " ]" )))
194
+ }
195
+
196
+ @ Test def slidingBufferExample () {
197
+ val open = Observable .interval(300 millis)
198
+ val closing = Observable .interval(600 millis)
199
+ val o = Observable .interval(100 millis).take(20 ).slidingBuffer(open)(_ => closing)
200
+ o.zipWithIndex.toBlocking.foreach {
201
+ case (seq, i) => println(s " Observable# $i emits $seq" )
202
+ }
194
203
}
195
204
196
205
@ Test def windowExample () {
@@ -199,10 +208,20 @@ class RxScalaDemo extends JUnitSuite {
199
208
).subscribe(output(_))
200
209
}
201
210
202
- @ Test def windowExample2 () {
211
+ @ Test def tumblingExample () {
203
212
val windowObservable = Observable .interval(500 millis)
204
213
val o = Observable .from(1 to 20 ).zip(Observable .interval(100 millis)).map(_._1)
205
- (for ((o, i) <- o.window(windowObservable).zipWithIndex; n <- o)
214
+ (for ((o, i) <- o.tumbling(windowObservable).zipWithIndex; n <- o)
215
+ yield s " Observable# $i emits $n"
216
+ ).toBlocking.foreach(println)
217
+ }
218
+
219
+ @ Test def slidingExample () {
220
+ val open = Observable .interval(300 millis)
221
+ val closing = Observable .interval(600 millis)
222
+ val o = Observable .interval(100 millis).take(20 ).sliding(open)(_ => closing)
223
+ (for ((o, i) <- o.zipWithIndex;
224
+ n <- o)
206
225
yield s " Observable# $i emits $n"
207
226
).toBlocking.foreach(println)
208
227
}
@@ -336,23 +355,22 @@ class RxScalaDemo extends JUnitSuite {
336
355
337
356
@ Test def groupByUntilExample () {
338
357
val numbers = Observable .interval(250 millis).take(14 )
339
- val grouped = numbers.groupByUntil[ Long ] (x => x % 2 , { case (key, obs) => obs.filter(x => x == 7 )})
358
+ val grouped = numbers.groupByUntil(x => x % 2 ){ case (key, obs) => obs.filter(x => x == 7 ) }
340
359
val sequenced = (grouped.map({ case (key, obs) => obs.toSeq })).flatten
341
360
sequenced.subscribe(x => println(s " Emitted group: $x" ))
342
361
}
343
362
344
363
@ Test def groupByUntilExample2 () {
345
364
val numbers = Observable .interval(250 millis).take(14 )
346
- val grouped = numbers.groupByUntil[ Long , Long ] (x => x % 2 , x => x * 10 , { case (key, obs) => Observable .interval(2 seconds)})
365
+ val grouped = numbers.groupByUntil(x => x % 2 , x => x * 10 ){ case (key, obs) => Observable .interval(2 seconds) }
347
366
val sequenced = (grouped.map({ case (key, obs) => obs.toSeq })).flatten
348
367
sequenced.toBlocking.foreach(x => println(s " Emitted group: $x" ))
349
368
}
350
369
351
370
@ Test def combineLatestExample () {
352
371
val firstCounter = Observable .interval(250 millis)
353
372
val secondCounter = Observable .interval(550 millis)
354
- val combinedCounter = firstCounter.combineLatest(secondCounter,
355
- (x : Long , y : Long ) => List (x,y)) take 10
373
+ val combinedCounter = firstCounter.combineLatestWith(secondCounter)(List (_, _)) take 10
356
374
357
375
combinedCounter subscribe {x => println(s " Emitted group: $x" )}
358
376
waitFor(combinedCounter)
@@ -363,7 +381,7 @@ class RxScalaDemo extends JUnitSuite {
363
381
val secondCounter = Observable .interval(550 millis)
364
382
val thirdCounter = Observable .interval(850 millis)
365
383
val sources = Seq (firstCounter, secondCounter, thirdCounter)
366
- val combinedCounter = Observable .combineLatest(sources, ( items : Seq [ Long ]) => items .toList).take(10 )
384
+ val combinedCounter = Observable .combineLatest(sources)(_ .toList).take(10 )
367
385
368
386
combinedCounter subscribe {x => println(s " Emitted group: $x" )}
369
387
waitFor(combinedCounter)
@@ -564,6 +582,12 @@ class RxScalaDemo extends JUnitSuite {
564
582
o1.zip(iter).toBlocking.foreach(println(_))
565
583
}
566
584
585
+ @ Test def zipWithExample () {
586
+ val xs = Observable .items(1 , 3 , 5 , 7 )
587
+ val ys = Observable .items(2 , 4 , 6 , 8 )
588
+ xs.zipWith(ys)(_ * _).subscribe(println(_))
589
+ }
590
+
567
591
@ Test def takeFirstWithCondition () {
568
592
val condition : Int => Boolean = _ >= 3
569
593
assertEquals(3 , List (1 , 2 , 3 , 4 ).toObservable.filter(condition).first.toBlocking.single)
@@ -1070,15 +1094,15 @@ class RxScalaDemo extends JUnitSuite {
1070
1094
1071
1095
@ Test def multicastExample1 (): Unit = {
1072
1096
val unshared = Observable .from(1 to 4 )
1073
- val shared = unshared.multicast(Subject ())
1097
+ val shared = unshared.multicast(Subject [ Int ] ())
1074
1098
shared.subscribe(n => println(s " subscriber 1 gets $n" ))
1075
1099
shared.subscribe(n => println(s " subscriber 2 gets $n" ))
1076
1100
shared.connect
1077
1101
}
1078
1102
1079
1103
@ Test def multicastExample2 (): Unit = {
1080
1104
val unshared = Observable .from(1 to 4 )
1081
- val shared = unshared.multicast[ Int , String ] (() => Subject (), o => o.map(" No. " + _))
1105
+ val shared = unshared.multicast(() => Subject [ Int ]())( o => o.map(" No. " + _))
1082
1106
shared.subscribe(n => println(s " subscriber 1 gets $n" ))
1083
1107
shared.subscribe(n => println(s " subscriber 2 gets $n" ))
1084
1108
}
@@ -1298,10 +1322,7 @@ class RxScalaDemo extends JUnitSuite {
1298
1322
1299
1323
@ Test def flatMapExample5 () {
1300
1324
val o = Observable .items(1 , 10 , 100 , 1000 )
1301
- o.flatMap(
1302
- (n : Int ) => Observable .interval(200 millis).take(5 ),
1303
- (n : Int , m : Long ) => n * m
1304
- ).toBlocking.foreach(println)
1325
+ o.flatMapWith(_ => Observable .interval(200 millis).take(5 ))(_ * _).toBlocking.foreach(println)
1305
1326
}
1306
1327
1307
1328
@ Test def flatMapIterableExample () {
@@ -1312,10 +1333,7 @@ class RxScalaDemo extends JUnitSuite {
1312
1333
1313
1334
@ Test def flatMapIterableExample2 () {
1314
1335
val o = Observable .items(1 , 10 , 100 , 1000 )
1315
- o.flatMapIterable(
1316
- (n : Int ) => (1 to 5 ),
1317
- (n : Int , m : Int ) => n * m
1318
- ).toBlocking.foreach(println)
1336
+ o.flatMapIterableWith(_=> (1 to 5 ))(_ * _).toBlocking.foreach(println)
1319
1337
}
1320
1338
1321
1339
@ Test def concatMapExample () {
@@ -1369,20 +1387,14 @@ class RxScalaDemo extends JUnitSuite {
1369
1387
@ Test def joinExample () {
1370
1388
val o1 = Observable .interval(500 millis).map(n => " 1: " + n)
1371
1389
val o2 = Observable .interval(100 millis).map(n => " 2: " + n)
1372
- val o = o1.join(o2,
1373
- (_ : String ) => Observable .timer(300 millis),
1374
- (_ : String ) => Observable .timer(200 millis),
1375
- (t1 : String , t2 : String ) => (t1, t2))
1390
+ val o = o1.join(o2)(_ => Observable .timer(300 millis), _ => Observable .timer(200 millis), (_, _))
1376
1391
o.take(10 ).toBlocking.foreach(println)
1377
1392
}
1378
1393
1379
1394
@ Test def groupJoinExample () {
1380
1395
val o1 = Observable .interval(500 millis).map(n => " 1: " + n)
1381
1396
val o2 = Observable .interval(100 millis).map(n => " 2: " + n)
1382
- val o = o1.groupJoin(o2,
1383
- (_ : String ) => Observable .timer(300 millis),
1384
- (_ : String ) => Observable .timer(200 millis),
1385
- (t1 : String , t2 : Observable [String ]) => (t1, t2.toSeq.toBlocking.single))
1397
+ val o = o1.groupJoin(o2)(_ => Observable .timer(300 millis), _ => Observable .timer(200 millis), (t1, t2) => (t1, t2.toSeq.toBlocking.single))
1386
1398
o.take(3 ).toBlocking.foreach(println)
1387
1399
}
1388
1400
0 commit comments