2
2
.
.
5
5
.
.
3
3
L
L
a
a
m
m
b
b
d
d
a
a
E
E
x
x
p
p
r
r
e
e
s
s
s
s
i
i
o
o
n
n
s
s
I
I
n
n
f
f
o
o
[
[
R
R
]
]
[
[
R
R
]
]
Lambda Expression (Lambda for short) declares Anonymous Function (Function that has no Name)
Each Function has Signature which defines
names and Data Types of Input Parameters (name:String, age:Int) (specifying Data Types is optional)
Data Type of Return Value (String) (specifying Data Type is optional)
Since Functions can be stored inside Constants and Variables (including Input Function Parameters) Signature is used to
say what kind of Closure can be stored into a Constant or Variable.
For instance var name : Int says that Variable name can store Int Data Type.
Similarly var closure : (String, Int) -> (String) says that Variable closure can store Closure Data Type of specified Signature -
only Closures which accept String & Int as Input Parameters and return String.
When you store Function inside Constant or Variable you can use them to call Function (without Named Parameters).
Parameter Names are only for internal use inside Function Body since Anonymous Functions can't have Named
Parameters when called.
Inside lambda we can only use qualified return syntax return@filter.
Otherwise, the value of the last expression is implicitly returned.
Content
Syntax
Lambda Expressions as Function Parameter
Lambda Expressions as Return Value
Lambda Expression
(String , Int) -> (String) //SIGNATURE OF ANONYMOUS FUNCTION
{ name:String, age:Int -> "$name is $age years old." } //DECLARATION & IMPLEMENTATION OF ANONYMOUS FUNCTION
{ name , age -> "$name is $age years old." } //DECLARATION & IMPLEMENTATION OF ANONYMOUS FUNCTION
greet("John" , 50) //Call with Indexed Parameters
S
S
y
y
n
n
t
t
a
a
x
x
Lambda Expression is given
inside curly brackets { ... } { ... }
starting by Function's Signature (name:String, age:Int) -> (accepts 2 Parameters)
followed by Function's Statements print("Hello"); "$name is $age years old."
Closure Syntax
//DECLARE VARIABLE THAT CAN HOLD FUNCTION DATA TYPE (OF SPECIFIED SIGNATURE).
var closureVariable : (String, Int) -> (String) //Closure that accepts 2 Parameters & returns String
//DECLARE ANONYMOUS FUNCTION. ASSIGN FUNCTION TO VARIABLE.
closureVariable = { name:String, age:Int -> print("Hello"); "$name is $age years old." }
closureVariable = { name , age -> "$name is $age years old." }
//CALL FUNCTION. STORE RETURN VALUE INTO VARIABLE.
var result : String = closureVariable("John", 20) //Closures don't have Named Parameters.
//DISPLAY RESULT.
print(result)
L
L
a
a
m
m
b
b
d
d
a
a
E
E
x
x
p
p
r
r
e
e
s
s
s
s
i
i
o
o
n
n
s
s
a
a
s
s
F
F
u
u
n
n
c
c
t
t
i
i
o
o
n
n
P
P
a
a
r
r
a
a
m
m
e
e
t
t
e
e
r
r
In this example Function executeFunction() accepts Function as Input Parameter. Function Signature is in red.
We will call this function twice
first we use Lambda Expression to store Anonymous Function into Variable and use this Variable as Parameter
then we skip the Variable part and use Lambda Expression directly in function call which stores declared Anonymous
Function directly into Function Input Parameter receivedFunction.
Lambda Expressions as Function Parameter
//===========================================================================================================
// FUNCTION: executeFunction
//===========================================================================================================
fun executeFunction(receivedFunction: (name: String, age: Int) -> String) {
var result = receivedFunction("John", 20)
println(result)
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//DECLARE LOCAL VARIABLES.
var weight = 150
//DECLARE VARIABLE TO HOLD FUNCTION DATA TYPE.
var anonymousFunction : (name: String, age: Int) -> String
//DECLARE ANONYMOUS FUNCTION USING LAMBDA EXPRESSION.
anonymousFunction = { name: String, age: Int -> "$name is $age years old and $weight kg." }
//CALL FUNCTION WITH FUNCTION AS PARAMETER (SENDS CLOSURE WITH weight Property).
executeFunction(anonymousFunction)
//USE LAMBDA EXPRESSION DIRECTLY AS PARAMETER.
executeFunction({ name: String, age: Int -> "$name IS $age YEARS OLD AND $weight KG." })
}
L
L
a
a
m
m
b
b
d
d
a
a
E
E
x
x
p
p
r
r
e
e
s
s
s
s
i
i
o
o
n
n
s
s
a
a
s
s
R
R
e
e
t
t
u
u
r
r
n
n
V
V
a
a
l
l
u
u
e
e
Body of the getFunction() can be replaced with just the Green part where in the single line
we use Lambda Expression to declare Anonymous Function
return Closure with
this Anonymous Function as its Method
Property weight which is declared outside of the Scope of Anonymous Function
Lambda Expressions as Return Value
//===========================================================================================================
// FUNCTION: getFunction
//===========================================================================================================
fun getFunction (weight: Int) : (name: String, age: Int) -> String {
//DECLARE VARIABLE TO HOLD FUNCTION DATA TYPE.
var anonymousFunction : (name: String, age: Int) -> String
//DECLARE ANONYMOUS FUNCTION USING LAMBDA EXPRESSION.
anonymousFunction = { name: String, age: Int -> "$name is $age years old and $weight kg." }
//RETURN CLOSURE WITH ANONYMOUS FUNCTION.
return anonymousFunction
//USE LAMBDA EXPRESSION DIRECTLY AS RETURN VALUE.
return { name: String, age: Int -> "$name is $age years old and $weight kg." }
}
//===========================================================================================================
// FUNCTION: main
//===========================================================================================================
fun main() {
//DECLARE VARIABLE TO HOLD FUNCTION DATA TYPE.
var returnedFunction : (name: String, age: Int) -> String
//DECLARE ANONYMOUS FUNCTION USING LAMBDA EXPRESSION.
returnedFunction = getFunction(150) //Returns Closure with partialy initialized Function
//CALL RETURNED FUNCTION.
var result = returnedFunction("John", 20) //Call Function with missing Parameters
//USE LAMBDA EXPRESSION AS PARAMETER.
print(result)
}