Skip to content

Commit 095910c

Browse files
authored
Merge pull request #110 from Bekz7/RxJava
RxJava
2 parents 33b7e46 + 7602917 commit 095910c

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed

kotlin-jvm/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies {
3939
implementation("io.github.lucapiccinelli:konad:_")
4040
implementation("io.github.serpro69:kotlin-faker:_")
4141
implementation("io.konform:konform:_")
42+
implementation("io.reactivex.rxjava3:rxjava:_")
4243
implementation("it.skrape:skrapeit-core:_")
4344
implementation("it.skrape:skrapeit-http-fetcher:_")
4445
implementation("org.jetbrains.exposed:exposed-core:_")
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
@file:Suppress("PackageDirectoryMismatch")
2+
3+
package playground.rxjava
4+
5+
import io.reactivex.rxjava3.core.Observable
6+
import io.reactivex.rxjava3.schedulers.Schedulers
7+
8+
/**
9+
* ReactiveX/RxJava - a library for composing asynchronous and event-based programs using observable sequences for the Java VM.
10+
*
11+
* - [ReactiveX/RxJava: GitHub](https://github.com/ReactiveX/RxJava)
12+
* - [JavaDoc](http://reactivex.io/RxJava/3.x/javadoc/)
13+
*/
14+
15+
fun main() {
16+
println()
17+
println("# RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.")
18+
19+
onlyAdultsObservable(persons()).subscribe(
20+
{ v -> println("Received: $v") },
21+
{ e -> println("Error: $e") },
22+
{ println("Complete!") }
23+
)
24+
25+
personsUnder30()
26+
concurrentPersonNames()
27+
numbers()
28+
}
29+
30+
private fun onlyAdultsObservable(persons: List<Person>) =
31+
Observable.create<Person> { emitter ->
32+
persons.forEach { person ->
33+
if (person.age < 18) {
34+
emitter.onError(Exception("The person is underage"))
35+
}
36+
emitter.onNext(person)
37+
}
38+
emitter.onComplete()
39+
}
40+
41+
private fun numbers() {
42+
Observable.range(1, 10)
43+
.map { e -> e * 10 }
44+
.subscribeOn(Schedulers.newThread())
45+
.blockingSubscribe(
46+
{ value -> println("Recived: $value") },
47+
{ error -> println("Error: $error") },
48+
{ println("Completed!") }
49+
)
50+
}
51+
52+
private fun personsUnder30() {
53+
Observable.fromIterable(persons())
54+
.filter { p -> p.age < 30 }
55+
.map { it.name.toUpperCase() }
56+
.subscribe(
57+
{ value -> println("Received: $value") },
58+
{ error -> println("Error: $error") },
59+
{ println("Completed!") }
60+
)
61+
}
62+
63+
private fun concurrentPersonNames() {
64+
val sherlock = Person("Sherlock", "Holmes", 20)
65+
val mycroft = Person("Mycroft", "Holmes", 30)
66+
val john = Person("John", "Watson", 25)
67+
68+
Observable.just(sherlock, mycroft, john)
69+
.flatMap { p -> Observable.just(p.name.toUpperCase()).subscribeOn(Schedulers.io()) }
70+
.subscribe(
71+
{ value -> println("Recived: $value") },
72+
{ error -> println("Error: $error") },
73+
{ println("Completed!") }
74+
)
75+
}
76+
77+
private fun persons() = listOf(
78+
Person("Sherlock", "Holmes", 20),
79+
Person("Mycroft", "Holmes", 30),
80+
Person("John", "Watson", 17)
81+
)
82+
83+
data class Person(var name: String, var lastName: String, var age: Int)

kotlin-jvm/src/main/kotlin/playground/_main.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ fun main() {
3838
playground.okio.main()
3939
playground.picnic.main()
4040
playground.retrofit.main()
41+
playground.rxjava.main()
4142
playground.skrapeit.main()
4243
playground.sqldelight.main()
4344
playground.statemachine.main()

versions.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ version.io.github.lucapiccinelli..konad=1.2.1
152152
version.io.github.serpro69..kotlin-faker=1.7.1
153153
## # available=1.8.0-rc.0
154154
version.io.konform..konform=0.3.0
155+
version.io.reactivex.rxjava3..rxjava=3.0.13
155156
version.it.skrape..skrapeit-core=1.0.0-alpha8
156157
version.it.skrape..skrapeit-http-fetcher=1.1.1
157158
version.junit=5.7.2

0 commit comments

Comments
 (0)