Reputation: 3924
I have been moving on to Kotlin for my Android Projects lately and have a problem understanding the Lambdas. When we convert a Java file to a Kotlin file we see many lambda expressions.
When and how should one exactly use a lambda function? Also, what is the difference between a higher order function and a lambda function/expression?
Answers specifically in context to Android would be helpful.
Upvotes: 2
Views: 2541
Reputation: 1986
For Lambda
{a:Int, b:Int -> a + b} // lambda of function type: (Int, Int) -> Int
// Equivalent function
fun sum(a:Int , b:Int) = a + b
--)We can assign lambda to a variable of similar function type:
var sum: (Int, Int) -> Int = {a:Int, b:Int -> a + b}
--)Which can be written without parameters types.
val sum: (Int, Int) -> Int = {a , b -> a + b}
println(sum(2,3)) // call sum
Lambda with single parameter is a special case.
The single parameter can be omitted along with the arrow ->
and use it as a reference to the single parameter.
Regular way:
var increment: (Int) -> Int = { a -> a + 1 }
Special case, single parameter is referenced as it
var increment: (Int) -> Int = { it + 1 }
val sum: (Int, Int) -> Int = {a , b ->
println("a = $a")
println("b = $b")
a + b // last expression is returned
}
from example,By default, the last expression of a lambda is implicitly returned.
Anonymous function is also a literal function which means it is not declared but passed as an expression. Anonymous function is a regular function without a name. Similar to regular functions, anonymous functions have function type.
var sum: (Int, Int) -> Int = fun(x: Int, y: Int): Int = x + y
println(sum(2,3)) // call sum
--)Higher-order functions can take functions as parameters or return a function.
--)The type of the parameter accepting the function or the return type is declared using function type.
// Higher-Order Function
fun higherOrderSum(a:Int, b:Int, f: (Int, Int) -> Int): Int{
return f(a,b)
}
typealias someType = (Int, Int) -> Int
fun main() {
val lambdaSum: someType = {a , b -> a + b}
println(higherOrderSum(2, 3, lambdaSum)) // 5
}
val view = findViewById(R.id.welcomeMessage)
view.setOnClickListener { v -> navigateWithView(v) }
As you can see, the left side defines the input values of the function (in this case a view), and the right side declares the operation that function will perform.
fun setOnClickListener(listener: (view: View) -> Unit){}
This is known as a Higher-Order Function, because it is a function that receives a function by parameter, or that returns a function.
Upvotes: 8
Reputation: 81879
When and how should one exactly use a lambda function? Also, what is the difference between a higher order function and a lambda function/expression?
A lambda is an anonymous function. It allows you to write functions without naming them.
A higher-order function is a function that takes another function as an argument (often passed as a lambda) or returns one as a result.
It’s difficult to tell you when a lambda actually should be used. As already seen, higher-order functions are probably the main use case. It’s a great way to pass „what should be done“ into another function. Many standard functions do use this. One simple example is forEach
on a collection:
listOf(1,2,3).forEach{
//tell what to do with each element in this lambda
}
Also a good place to find lambdas is DSLs like the Anko library includes one. These constructs also make heavy use of lambdas.
Upvotes: 6