- Reactive Programming in Kotlin
- Rivu Chakraborty
- 368字
- 2021-07-02 22:26:40
Functional programming – monads
Functional programming is incomplete without monads. If you are into functional programming, then you know it very well; otherwise, you are hearing it for the first time. So, what is a monad? Let's learn about it. The concept of monad is quite abstract; the definition says monad is a structure that creates a new type by encapsulating a value and adding some extra functionalities to it. So, let's start by using a monad; take a look at the following program:
fun main(args: Array<String>) { val maybeValue: Maybe<Int> = Maybe.just(14)//1 maybeValue.subscribeBy(//2 onComplete = {println("Completed Empty")}, onError = {println("Error $it")}, onSuccess = { println("Completed with value $it")} ) val maybeEmpty:Maybe<Int> = Maybe.empty()//3 maybeEmpty.subscribeBy( onComplete = {println("Completed Empty")}, onError = {println("Error $it")}, onSuccess = { println("Completed with value $it")} ) }
Here, Maybe is a monad that encapsulates an Int value with some added functionalities. The Maybe monad says it may or may not contain a value, and it completes with or without a value or with an error. So, if there's an error, then it would obviously call onError; if there are no errors, and if it has a value, it will call onSuccess with the value; and, if it doesn't have a value and no error as well, it will call onComplete. The thing to note is that all three methods here, onError, onComplete, and onSuccess, are terminal methods, meaning either one of these three will get called by a Maybe monad, and others will never be called.
Let's go through the program to understand the monads better. On comment (1), we will declare a Maybe monad and assign a value of 14 to it. On comment (2), we will subscribe to the monad. On comment (3), we will again declare a Maybe monad, this time with an empty value. The subscription takes three lambdas as parameter–when the monad contains a value, onSuccess gets called; when it doesn't contain any value, onComplete gets called; and if any error occurs, then onError gets called. Let's see the output now:
Completed with value 14 Completed Empty
So, as we can see, for maybeValue, onSuccess gets called, but for maybeEmpty , the onComplete method gets called.
- GitHub Essentials
- Access 2016數(shù)據(jù)庫教程(微課版·第2版)
- 工業(yè)大數(shù)據(jù)分析算法實戰(zhàn)
- 數(shù)據(jù)庫應(yīng)用基礎(chǔ)教程(Visual FoxPro 9.0)
- 大數(shù)據(jù)技術(shù)入門
- 數(shù)據(jù)中心數(shù)字孿生應(yīng)用實踐
- LabVIEW 完全自學(xué)手冊
- Apache Kylin權(quán)威指南
- 圖數(shù)據(jù)實戰(zhàn):用圖思維和圖技術(shù)解決復(fù)雜問題
- Construct 2 Game Development by Example
- 探索新型智庫發(fā)展之路:藍迪國際智庫報告·2015(下冊)
- Splunk智能運維實戰(zhàn)
- Visual Studio 2013 and .NET 4.5 Expert Cookbook
- Access數(shù)據(jù)庫開發(fā)從入門到精通
- Internet of Things with Python