Skip to content

Instantly share code, notes, and snippets.

@senamit2708
Created July 18, 2023 07:43
Show Gist options
  • Save senamit2708/64add2cacf232b8ed8abd15e59cd81c4 to your computer and use it in GitHub Desktop.
Save senamit2708/64add2cacf232b8ed8abd15e59cd81c4 to your computer and use it in GitHub Desktop.
lambda function learning
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