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
//Name: (only comma separated data types) -> return data type | |
ft: (Int, Int) -> Int |
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
ft(1, 2) | |
//OR | |
ft.invoke(1, 2) |
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 doSomething(a: Int, b: Int, ft: (Int, Int) -> Int): String { | |
val result = ft(a, b) //OR ft.invoke(a, b) | |
return “doSomething: ” + result | |
} |
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
//Calling a higher order function | |
println(doSomething(1, 2, {x: Int, y: Int -> x + y})) | |
//prints: doSomething: 3 |
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
//nameOfTheLambda: function type = { comma separated pascal parameters -> business logic } | |
val lambda: (Int, Int): Int = { x: Int, y: Int -> x + y } |
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
//Normal function | |
fun increment (x: Int): Int { | |
return x + 1 | |
} | |
//The equivalent lambda expression for above function | |
val lambda: (Int): Int = { x -> x + 1 } | |
//The equivalent short lambda expression for above single parameter lambda | |
val lambda: (Int): Int = { it + 1 } |
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
println(lambda(1,2)) //prints: 3 |
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
println(doSomething(1, 2, lambda)) //prints: doSomething: 3 |
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(comma separated pascal parameters) = business logic | |
fun(x: Int, y: Int) = x + y |
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
//Storing an anonymous function in a variable | |
val anonymousFunction: (Int, Int): Int = fun(x: Int, y: Int) = x + y |