Applying functional programming to the ReactiveCalculator class
So, now, after trying to understand the ReactiveCalculator
class from the previous chapter, we will try to optimize the code as well. Let's first take a look at the init
block of the ReactiveCalculator
class:
init{ nums = Pair(a,b) subjectAdd.map({ it.first+it.second }).subscribe({println ("Add = $it")} )//1 subjectSub.map({ it.first-it.second }).subscribe({println ("Substract = $it")} ) subjectMult.map({ it.first*it.second }).subscribe ({println("Multiply = $it")} ) subjectDiv.map({ it.first/(it.second*1.0) }).subscribe ({println("Divide = $it")} ) subjectCalc.subscribe({ with(it) { calculateAddition() calculateSubstraction() calculateMultiplication() calculateDivision() } }) subjectCalc.onNext(this) }
So, now, with the knowledge of functional programming, we can easily...