kotlin function literal code example
Example: kotlin function literal
val add: (Int, Int) -> Int = { i, j -> i + j }
// Simple lambda expression
val printAndDouble: (Int) -> Int = {
println(it)
// When single parameter, we can reference it using `it`
it * 2 // In lambda, last expression is returned
}
// Anonymous function alternative
val printAndDoubleFun: (Int) -> Int = fun(i: Int): Int {
println(i) 6LQJOHDUJXPHQWFDQWEHUHIHUHQFHGE\CLWC
return i * 2 // Needs return like any function
}
val i = printAndDouble(10) // 10
print(i) // 2