This file contains hidden or 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
val list = listOf(1, 3, 5, 7, 9) | |
// if the lambda expression is the last parameter, we can move the parameter out | |
println(list.fold(1) { a, b -> a * b }) |
This file contains hidden or 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
val list = listOf(1, 3, 5, 7, 9) | |
// fold function is taking a parameter and a lambda expression | |
println(list.fold(1, { a, b -> a * b })) |
This file contains hidden or 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() { | |
areaOfRectangleSimplified(30,40) | |
} |
This file contains hidden or 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
//simplified version | |
val areaOfRectangleSimplified: (Int, Int) -> Int = { length, breadth -> length * breadth } |
This file contains hidden or 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() { | |
areaOfRectangle(10,20) | |
} |
This file contains hidden or 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
//Lambda Expression | |
val areaOfRectangle = { length: Int, breadth: Int -> (length * breadth) } |
This file contains hidden or 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
{ length: Int, breadth: Int -> (length * breadth) } |
This file contains hidden or 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 area(length:Int, breadth: Int) : Int{ | |
return length * breadth | |
} |
This file contains hidden or 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 areaOfSquare = area() | |
println(areaOfSquare(5)) | |
println(areaOfSquare(10)) | |
} | |
fun area() : (Int) -> Int { | |
return {i -> i * i} | |
} |
This file contains hidden or 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 list = listOf(1,2,3,4,5) | |
list.forEach({a -> a*a}) | |
} |