Lambdas function and high order function
what is Lambdas function?
Lambdas function are the function that doesnt have a function name they are defined by using curly braces it contain obj name followed by data type and return type and inside the curly braces with argument list and body of operation.
val lamdasname : (Datatype) -> Returntype = { argumentlis
->bodyofcode}
Lambda function with no return type mention take its return type based on last line from bodyofcode example below
val add = {a:Int,b:Int -> a+b}
println(add(3,2))
Lambda function with two data type of integer number and return type of integer value mentioned
val add:(Int,Int)->Int = {a:Int,b:Int -> a+b}
println(add(3,2))
What is Higher-Order Functions?
High-Order Functions are the one which we can pass our lambdas or anonymous function as an parameter and we can also return function.
Example of passing lambdas function as an parameter
val add:(Int,Int)->Int = {a:Int,b:Int -> a+b}
hof(add)
fun hof(adds:(Int,Int) -> Int)
{
println(adds(3,2))
}
Example of Returning a function from Higher-Order function
fun main()
{
val multiplys = multiplyvalue()
// invokes the multiply() function by passing arguments
val result = multiplys(2,4)
println(result)
}fun multiplyvalue():((Int,Int) -> Int)
{
return ::multiply
}
fun multiply(a:Int,b:Int):Int
{
return a*b
}