- Reactive Programming in Kotlin
- Rivu Chakraborty
- 249字
- 2021-07-02 22:26:36
What is reactive programming?
Reactive programming is an asynchronous programming paradigm that revolves around data streams and the propagation of change. In simpler words, those programs which propagate all the changes that affected its data/data streams to all the interested parties (such as end users, components and sub-parts, and other programs that are somehow related) are called reactive programs.
For example, take any spreadsheet (say the Google Sheet), put any number in the A1 cell, and in the B1 cell, write the =ISEVEN(A1) function; it'll show TRUE or FALSE, depending on whether you've entered an even or odd number. Now, if you modify the number in A1, the value of B1 will also get changed automatically; such behavior is called reactive.
Not clear enough? Let's look at a coding example and then try to understand it again. The following is a normal Kotlin code block to determine if a number is even or odd:
fun main(args: Array<String>) { var number = 4 var isEven = isEven(number) println("The number is " + (if (isEven) "Even" else "Odd")) number = 9 println("The number is " + (if (isEven) "Even" else "Odd")) } fun isEven(n:Int):Boolean = ((n % 2) == 0)
If you check the output of the program, then you'll see that, although the number is assigned a new value, isEven is still true; however, if isEven was made to track changes of the number, then it would automatically become false. A reactive program would just do the same.
- LibGDX Game Development Essentials
- Greenplum:從大數據戰略到實現
- 跟老男孩學Linux運維:MySQL入門與提高實踐
- 從0到1:JavaScript 快速上手
- 新基建:數據中心創新之路
- 達夢數據庫運維實戰
- Hadoop集群與安全
- MySQL技術內幕:SQL編程
- 企業主數據管理實務
- Mastering ROS for Robotics Programming(Second Edition)
- 數據庫查詢優化器的藝術:原理解析與SQL性能優化
- Spring Boot 2.0 Cookbook(Second Edition)
- 數據分析思維:產品經理的成長筆記
- SQL Server 數據庫教程(2008版)
- SQL Server 2012數據庫技術及應用(第4版)