Reactive programming relies on asynchronous streams of data that can be observed, transformed, and responded to. Reactive programming has gained a great deal of popularity recently with the adoption of Redux and ReactiveX across various languages.
For Java developers, RxJava has become quite popular, especially in the Android development domain. Because of Kotlin's strong interoperability with Java, it's also possible to leverage RxJava and RxKotlin to write reactive code.
The following example demonstrates one way in which reactive code can be written in Kotlin:
studentsObservable
.filter { student -> student.grade == 11 }
.filter { student -> student.gpa > 3.5 }
.subscribeBy(
onNext = { displayStudents(it) },
onError = { error -> error.printStackTrace() },
onComplete = { println("Done!") }
)
Kotlin supports reactive programming via multiple means such as RxJava/RxKotlin and coroutine channels.
In Chapter 3, Understanding Programming Paradigms in Kotlin, you'll dive deeper into the specifics of each programming paradigm and how they are influenced and realized in Kotlin.
Now that we understand how to pick a programming paradigm, let's move on to find out about first-class functions and how they fit in.