- 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.
- Architects of Intelligence
- Effective Amazon Machine Learning
- Python數(shù)據(jù)分析、挖掘與可視化從入門到精通
- MySQL從入門到精通(第3版)
- 達(dá)夢(mèng)數(shù)據(jù)庫(kù)性能優(yōu)化
- 大數(shù)據(jù)架構(gòu)和算法實(shí)現(xiàn)之路:電商系統(tǒng)的技術(shù)實(shí)戰(zhàn)
- 數(shù)據(jù)庫(kù)技術(shù)及應(yīng)用教程
- 深入淺出 Hyperscan:高性能正則表達(dá)式算法原理與設(shè)計(jì)
- MATLAB Graphics and Data Visualization Cookbook
- Python數(shù)據(jù)分析與挖掘?qū)崙?zhàn)(第3版)
- SQL Server 2012實(shí)施與管理實(shí)戰(zhàn)指南
- R Machine Learning Essentials
- Unity Game Development Blueprints
- Arquillian Testing Guide
- 大數(shù)據(jù)計(jì)算系統(tǒng)原理、技術(shù)與應(yīng)用