Lambda Expressions
Haim Michael
June 8th
, 2021
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
life
michae
l
www.lifemichael.com
in
Kotlin
Ā© 2008 Haim Michael 20150805
XtremePython Conference
https://xtremepython.dev
This Conference will be in English
Ā© 2008 Haim Michael 20150805
XtremeJS Conference
https://xtremejs.dev
This Conference will be in English
Ā© 2008 Haim Michael 20150805
Courses on Udemy
https://www.udemy.com/user/life-michael/
Online Courses in English
Ā© 2008 Haim Michael 20150805
Online Courses
Https://academy.lifemichael.com
Online Courses in English and in Hebrew
Ā© 2008 Haim Michael 20150805
Online Courses for Kids
http://kids.lifemichael.com
Online Courses in English and in Hebrew
Ā© 2008 Haim Michael 20150805
Premium Training Services
https://lifemichael.com
Professional Services in English and in Hebrew
Ā© 2008 Haim Michael 20150805
Scala Fundamentals Course
http://scala.course.lifemichael.com
This Course is delivered in Hebrew
Ā© 2008 Haim Michael 20150805
Kotlin Fundamentals Course
http://kotlin.course.lifemichael.com
This Course is delivered in Hebrew
Ā© 2008 Haim Michael 20150805
Functional Programming in Java
http://functional.java.seminar.lifemichael.com/
This Seminar is delivered in Hebrew
Ā© 2008 Haim Michael 20150805
Table of Contents
 We are going to cover the following topics:
Introduction to Lambda
Higher Order Functions
Function Types in Kotlin
The Parameters' Names
Combining Function Types
Instantiating Function Types
Compiler Inference
Invoking Function Type Objects
Function Literals
Lambda Expressions Syntax
The Function Last Parameter
The it Implicit Parameter
The :: Operator
The Underscore Style
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 12
Introduction
 The functions in Kotlin can be first class, which means that
we can handle objects that represent functionality. We can
store the references for those object into variables, pass the
references as arguments to other functions or get them as
the returned value when calling a function.
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 13
Higher Order Functions
 Higher order function is a function that takes functions as
arguments and/or returns a function.
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 14
Function Types
 Kotlin uses a family of function types for declarations that
deal with functions.
val onLoad: () → Unit = …
 The function type has a parenthesized parameter types list
and a return type:
(A,B) -> C
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 15
Function Types
 The function type can optionally have an additional receiver
type, specified before a dot.
A.(B) -> C
This type represents a function we can invoke on object of
type A, with parameter of type B, and a returned value of
type C.
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 16
Function Types
 Using the suspend modifier we can indicate the function is
a suspending one.
suspend () -> Unit
suspend A.(B) -> C
 Suspending functions can be called from within coroutines
only.
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 17
Function Types
 In order to specify a function type is nullable we should
place it all in parentheses and add the question mark.
((Int,Int)->Int)?
 We can give a function type an alternative alias by using the
typealias keyword.
typealias Calculate = (Int,Int) ->Int
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 18
Parameters Names
 The function type notation can include names for the
parameters. Using such names assists with the creation of
accurate documentation.
(height:Int, width:Int) -> Rectangle
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 19
Combining Function Types
 It is possible to combine function types and use parentheses
in order to get code that is easier to understand.
(Int) -> ((int)->Unit)
 The arrow notation is right associative, which enables us to
write alternatively the following:
(Int) -> (Int) -> Unit
`
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 20
Instantiate Function Type
 There are several ways for instantiating a function type. We
can do it using a lambda expression.
{a,b -> a+b}
 We can do it using an anonymous function.
fun(s:String):Int {return s.length}
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 21
Instantiate Function Type
 We can declare a class that implement a specific function
type as an interface.
 We can assign a function type variable with a reference for
an object we instantiate from that class.
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 22
Instantiate Function Type
class X : (Int) -> Int {
override fun invoke(x:Int):Int = 10*x
}
var f = X()
fun main(args: Array<String>) {
print(f(4))
}
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 23
Compiler Inference
 The compiler can infer the function types of variable when
enough information is available.
val f = {num:Int -> num + 1}
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 24
Function Type with Receivers
 The values of function types with and without receiver are
interchangeable. The receiver can stand for the first
parameter.
var f1:String.(Int)->String = {num -> this.repeat(num)}
var f2:(String,Int)->String = f1
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 25
Invoking Function Type Instances
 We can invoke it either using the invoke(...) method or
just by using simple parentheses.
class X : (Int) -> Int {
override fun invoke(x:Int):Int = 10*x
}
var f = X()
fun main(args: Array<String>) {
print(f(4))
print(f.invoke(4))
}
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 26
Function Literals
 Lambda expressions and anonymous functions are
considered as function literals.
 Functions literal are functions that are not declared and can
be passed over.
max (numbers, {a,b->a<b})
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 27
Lambda Expressions Syntax
 The full syntactic form of a lambda expression includes the
use of the curly braces. The lambda expression is always
surrounded by curly braces.
val f = {x:Int,y:Int->x+y}
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 28
Lambda Expressions Syntax
 If the return type of the lambda expression is not Unit, then
the last (or possibly single) expression inside the lambda
body is treated as the returned value.
val sum : (Int,Int)->Int = {x,y->x+y};
val sum : (Int,Int)->Int = {x,y->
var result = x + y
result
};
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 29
The Function Last Parameter
 If the last parameter of a function accepts a function, a
lambda expression that is passed over as the corresponding
argument to that parameter, can be placed outside of the
parentheses:
val ob = accounts.doSomething(0) {total,e → total *e}
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 30
The it Implicit Parameter
 When the lambda expression has one parameter only, it is
allows to omit the arrow and implicitly name the parameter
with it.
numbers.filter(it%2==0)
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 31
The :: Operator
 When prepending a function name with the '::' operator we
will get a reference for the object that represents the
function.
 We can then assign that referent to a variable that its type is
a function type.
fun add(a: Int, b: Int) = a + b
var f: (n1:Int,n2:Int)->Int = ::add
println(f(3,4)) //prints 7
Ā© 2009 Haim Michael All Rights Reserved 3.1.2021 32
The Underscore Style
 When writing a lambda expression and having a parameter
we don't use we can place an underscore instead.
map.forEach {_,v -> println(ā€œ$v is awesome!ā€œ}
Ā© 2009 Haim Michael All Rights Reserved 33
Questions & Answers
Thanks for Your Time!
Haim Michael
haim.michael@lifemichael.com
+972+3+3726013 ext:700
+972+54+6655837 (whatsapp)
life
michael

Kotlin lambda expressions_20210608

  • 1.
    Lambda Expressions Haim Michael June8th , 2021 All logos, trade marks and brand names used in this presentation belong to the respective owners. life michae l www.lifemichael.com in Kotlin
  • 2.
    Ā© 2008 HaimMichael 20150805 XtremePython Conference https://xtremepython.dev This Conference will be in English
  • 3.
    Ā© 2008 HaimMichael 20150805 XtremeJS Conference https://xtremejs.dev This Conference will be in English
  • 4.
    Ā© 2008 HaimMichael 20150805 Courses on Udemy https://www.udemy.com/user/life-michael/ Online Courses in English
  • 5.
    Ā© 2008 HaimMichael 20150805 Online Courses Https://academy.lifemichael.com Online Courses in English and in Hebrew
  • 6.
    Ā© 2008 HaimMichael 20150805 Online Courses for Kids http://kids.lifemichael.com Online Courses in English and in Hebrew
  • 7.
    Ā© 2008 HaimMichael 20150805 Premium Training Services https://lifemichael.com Professional Services in English and in Hebrew
  • 8.
    Ā© 2008 HaimMichael 20150805 Scala Fundamentals Course http://scala.course.lifemichael.com This Course is delivered in Hebrew
  • 9.
    Ā© 2008 HaimMichael 20150805 Kotlin Fundamentals Course http://kotlin.course.lifemichael.com This Course is delivered in Hebrew
  • 10.
    Ā© 2008 HaimMichael 20150805 Functional Programming in Java http://functional.java.seminar.lifemichael.com/ This Seminar is delivered in Hebrew
  • 11.
    © 2008 HaimMichael 20150805 Table of Contents  We are going to cover the following topics: Introduction to Lambda Higher Order Functions Function Types in Kotlin The Parameters' Names Combining Function Types Instantiating Function Types Compiler Inference Invoking Function Type Objects Function Literals Lambda Expressions Syntax The Function Last Parameter The it Implicit Parameter The :: Operator The Underscore Style
  • 12.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 12 Introduction  The functions in Kotlin can be first class, which means that we can handle objects that represent functionality. We can store the references for those object into variables, pass the references as arguments to other functions or get them as the returned value when calling a function.
  • 13.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 13 Higher Order Functions  Higher order function is a function that takes functions as arguments and/or returns a function.
  • 14.
    Ā© 2009 HaimMichael All Rights Reserved 3.1.2021 14 Function Types  Kotlin uses a family of function types for declarations that deal with functions. val onLoad: () → Unit = …  The function type has a parenthesized parameter types list and a return type: (A,B) -> C
  • 15.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 15 Function Types  The function type can optionally have an additional receiver type, specified before a dot. A.(B) -> C This type represents a function we can invoke on object of type A, with parameter of type B, and a returned value of type C.
  • 16.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 16 Function Types  Using the suspend modifier we can indicate the function is a suspending one. suspend () -> Unit suspend A.(B) -> C  Suspending functions can be called from within coroutines only.
  • 17.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 17 Function Types  In order to specify a function type is nullable we should place it all in parentheses and add the question mark. ((Int,Int)->Int)?  We can give a function type an alternative alias by using the typealias keyword. typealias Calculate = (Int,Int) ->Int
  • 18.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 18 Parameters Names  The function type notation can include names for the parameters. Using such names assists with the creation of accurate documentation. (height:Int, width:Int) -> Rectangle
  • 19.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 19 Combining Function Types  It is possible to combine function types and use parentheses in order to get code that is easier to understand. (Int) -> ((int)->Unit)  The arrow notation is right associative, which enables us to write alternatively the following: (Int) -> (Int) -> Unit `
  • 20.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 20 Instantiate Function Type  There are several ways for instantiating a function type. We can do it using a lambda expression. {a,b -> a+b}  We can do it using an anonymous function. fun(s:String):Int {return s.length}
  • 21.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 21 Instantiate Function Type  We can declare a class that implement a specific function type as an interface.  We can assign a function type variable with a reference for an object we instantiate from that class.
  • 22.
    Ā© 2009 HaimMichael All Rights Reserved 3.1.2021 22 Instantiate Function Type class X : (Int) -> Int { override fun invoke(x:Int):Int = 10*x } var f = X() fun main(args: Array<String>) { print(f(4)) }
  • 23.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 23 Compiler Inference  The compiler can infer the function types of variable when enough information is available. val f = {num:Int -> num + 1}
  • 24.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 24 Function Type with Receivers  The values of function types with and without receiver are interchangeable. The receiver can stand for the first parameter. var f1:String.(Int)->String = {num -> this.repeat(num)} var f2:(String,Int)->String = f1
  • 25.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 25 Invoking Function Type Instances  We can invoke it either using the invoke(...) method or just by using simple parentheses. class X : (Int) -> Int { override fun invoke(x:Int):Int = 10*x } var f = X() fun main(args: Array<String>) { print(f(4)) print(f.invoke(4)) }
  • 26.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 26 Function Literals  Lambda expressions and anonymous functions are considered as function literals.  Functions literal are functions that are not declared and can be passed over. max (numbers, {a,b->a<b})
  • 27.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 27 Lambda Expressions Syntax  The full syntactic form of a lambda expression includes the use of the curly braces. The lambda expression is always surrounded by curly braces. val f = {x:Int,y:Int->x+y}
  • 28.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 28 Lambda Expressions Syntax  If the return type of the lambda expression is not Unit, then the last (or possibly single) expression inside the lambda body is treated as the returned value. val sum : (Int,Int)->Int = {x,y->x+y}; val sum : (Int,Int)->Int = {x,y-> var result = x + y result };
  • 29.
    Ā© 2009 HaimMichael All Rights Reserved 3.1.2021 29 The Function Last Parameter  If the last parameter of a function accepts a function, a lambda expression that is passed over as the corresponding argument to that parameter, can be placed outside of the parentheses: val ob = accounts.doSomething(0) {total,e → total *e}
  • 30.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 30 The it Implicit Parameter  When the lambda expression has one parameter only, it is allows to omit the arrow and implicitly name the parameter with it. numbers.filter(it%2==0)
  • 31.
    © 2009 HaimMichael All Rights Reserved 3.1.2021 31 The :: Operator  When prepending a function name with the '::' operator we will get a reference for the object that represents the function.  We can then assign that referent to a variable that its type is a function type. fun add(a: Int, b: Int) = a + b var f: (n1:Int,n2:Int)->Int = ::add println(f(3,4)) //prints 7
  • 32.
    Ā© 2009 HaimMichael All Rights Reserved 3.1.2021 32 The Underscore Style  When writing a lambda expression and having a parameter we don't use we can place an underscore instead. map.forEach {_,v -> println(ā€œ$v is awesome!ā€œ}
  • 33.
    Ā© 2009 HaimMichael All Rights Reserved 33 Questions & Answers Thanks for Your Time! Haim Michael haim.michael@lifemichael.com +972+3+3726013 ext:700 +972+54+6655837 (whatsapp) life michael