Created
July 18, 2023 07:43
-
-
Save senamit2708/64add2cacf232b8ed8abd15e59cd81c4 to your computer and use it in GitHub Desktop.
lambda function learning
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(){ | |
var fn: (a: Int, b: Int) -> Int = ::sum | |
var lambdaOne: (Int,Int)->Int = {x: Int, y: Int -> x+y } | |
multiLineLambda() | |
val singleParameter: (Int) -> (Int) = {x: Int -> x * x} | |
val simplifySingleParameter: (Int) -> (Int) = {it * it} | |
calculator(1, 2){a,b -> a +b} //if lambda expression is the last parameter of a function | |
//you can write that lamdba in above way also. | |
} | |
val multiLineLambda: () -> Int = { | |
println("Hello lambda") | |
val a: Int = 2 + 3 | |
"Hello lambda code" | |
a//last statement is automatically the return statement in lambda. | |
} | |
fun sum(a: Int, b: Int) : Int = a + b | |
fun calculator( a: Int, b: Int, op: (Int, Int) -> Int): Int { | |
return op(a,b) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment