This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
val sum = { x: Int, y: Int -> x + y } // (1) | |
println("Sum ${sum(12,14)}")// (2) | |
val anonymousMult = {x: Int -> (Random().nextInt(15)+1) * x}// (3) | |
println("random output ${anonymousMult(2)}")// (4) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReactiveCalculator(a:Int, b:Int) { | |
private val subjectAdd: Subject<Pair<Int, Int>> = PublishSubject.create() | |
private val subjectSub: Subject<Pair<Int, Int>> = PublishSubject.create() | |
private val subjectMult: Subject<Pair<Int, Int>> =PublishSubject.create() | |
private val subjectDiv: Subject<Pair<Int, Int>> = PublishSubject.create() | |
private val subjectCalc: Subject<ReactiveCalculator> = PublishSubject.create() | |
private var nums:Pair<Int,Int> = Pair(0,0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
init{ | |
nums = Pair(a,b) | |
subjectAdd.map { it.first+it.second }.subscribe {println("Add = $it")} | |
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ReactiveCalculator(a:Int, b:Int) { | |
private val subjectAdd: Subject<Pair<Int, Int>> = PublishSubject.create() | |
private val subjectSub: Subject<Pair<Int, Int>> = PublishSubject.create() | |
private val subjectMult: Subject<Pair<Int, Int>> =PublishSubject.create() | |
private val subjectDiv: Subject<Pair<Int, Int>> = PublishSubject.create() | |
private val subjectCalc: Subject<ReactiveCalculator> = PublishSubject.create() | |
private var nums:Pair<Int,Int> = Pair(0,0) | |
... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main(args: Array<String>) { | |
println("Initial Out put with a = 15, b = 10") | |
val calculator = ReactiveCalculator(15, 10) | |
println("Enter a = <number> or b = <number> in separate lines\nexit to exit the program") | |
var line:String? | |
do { | |
line = readLine(); | |
calculator.handleInput(line) | |
} while (line!= null && !line.toLowerCase().contains("exit")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/build |