- Reactive Programming in Kotlin
- Rivu Chakraborty
- 186字
- 2021-07-02 22:26:37
The ReactiveEvenOdd program
So, now that we are somewhat familiar with observables, let's modify the even-odd program in a reactive way. Here is the code for doing so:
fun main(args: Array<String>) { var subject:Subject<Int> = PublishSubject.create() subject.map({ isEven(it) }).subscribe({println
("The number is ${(if (it) "Even" else "Odd")}" )}) subject.onNext(4) subject.onNext(9) }
Here is the output:

In this program, we have used subject and map, which we will cover in the later chapters. Here, it is just to show how easy it is in reactive programming to notify the changes. If you look at the program closely, then you'll also find that the code is modular and functional. When we notify subject with a number, it calls the method in map, then it calls the method in subscribe with the return value of the map method. The map method checks if the number is even and returns true or false accordingly; in the subscribe method, we are receiving that value and printing even or odd accordingly. The subject.onNext method is the way through which we message the new value to the subject, so it can process it.
- Hands-On Data Structures and Algorithms with Rust
- 從零開始學Hadoop大數據分析(視頻教學版)
- Visual Studio 2015 Cookbook(Second Edition)
- 新型數據庫系統:原理、架構與實踐
- 文本數據挖掘:基于R語言
- 工業大數據分析算法實戰
- 大數據Hadoop 3.X分布式處理實戰
- WS-BPEL 2.0 Beginner's Guide
- Google Cloud Platform for Developers
- 智慧城市中的大數據分析技術
- 云計算
- 數據挖掘與機器學習-WEKA應用技術與實踐(第二版)
- 基于數據發布的隱私保護模型研究
- Applying Math with Python
- MySQL技術內幕:InnoDB存儲引擎(第2版)