2 Functions
2 Functions
Luca Verderame
About this lesson
● (Almost) Everything has a value
● Functions in Kotlin
● Compact functions
● Lambdas and higher-order functions
● List filters
● Summary
2
(Almost) Everything has a value
3
(Almost) Everything is an expression
In Kotlin, almost everything is an expression and has a value. Even an if
expression has a value.
val temperature = 20
val isHot = if (temperature > 40) true else false
println(isHot)
⇒ false
4
Expression values
Sometimes, that value is kotlin.Unit.
⇒ This is an expression
kotlin.Unit
5
Functions in Kotlin
6
About functions
● A block of code that performs a specific task
7
Parts of a function
Earlier, you created a simple function that printed "Hello World".
fun printHello() {
println("Hello World")
}
printHello()
8
Unit returning functions
If a function does not return any useful value, its return type is Unit.
9
Unit returning functions
The Unit return type declaration is optional.
fun printHello(name: String?): Unit {
println("Hi there!")
}
is equivalent to:
fun printHello(name: String?) {
println("Hi there!")
}
10
Function arguments
Functions may have:
● Default parameters
● Required parameters
● Named arguments
11
Default parameters
Default values provide a fallback if no parameter value is passed.
12
Required parameters
If no default is specified for a parameter, the corresponding argument is
required.
Required parameters
13
Default versus required parameters
Functions can have a mix of default and required parameters.
14
Named arguments
To improve readability, use named arguments for required arguments.
It's considered good style to put default arguments after positional arguments,
that way callers only have to specify the required arguments.
15
Compact functions
16
Single-expression functions
Compact functions, or single-expression functions, make your code more
concise and readable.
17
Lambdas and higher-order functions
18
Kotlin functions are first-class
● Kotlin functions can be stored in variables and data structures
19
Syntax for function types
Kotlin's syntax for function types is closely related to its syntax for lambdas.
Declare a variable that holds a function.
20
Lambda functions
A lambda is an expression that makes a function that has no name.
21
Higher-order functions
Higher-order functions take functions as parameters, or return a function.
The body of the code calls the function that was passed as the second argument,
and passes the first argument along to it.
22
Higher-order functions
To call this function, pass it a string and a function.
23
Passing a function reference
Use the :: operator to pass a named function as an argument to another
function.
The :: operator lets Kotlin know that you are passing the function reference as an
argument, and not trying to call the function.
You can pass a lambda as a function parameter without putting it inside the
parentheses.
25
Using higher-order functions
Many Kotlin built-in functions are defined using last parameter call syntax.
26
List filters
27
List filters
Get part of a list based on some condition
28
Iterating through lists
If a function literal has only one parameter, you can omit its declaration and
the "->". The parameter is implicitly declared under the name it.
Filter iterates through a collection, where it is the value of the element during the
iteration. This is equivalent to:
29
List filters
The filter condition in curly braces {} tests each item as the filter loops through.
If the expression returns true, the item is included.
val books = listOf("nature", "biology", "birds")
println(books.filter { it[0] == 'b' })
⇒ [biology, birds]
30
Other list transformations
● map() performs the same transform on every item and returns the list.
31
References
● Lambdas
● Lambda Expressions and Anonymous Functions
● Higher-order functions
● Collection Transformation Operations
Extra Topic: Eager and lazy filters
33
Eager and lazy filters
Evaluation of expressions in lists:
Lazy evaluation of lists is useful if you don't need the entire result, or if the list is
exceptionally large and multiple copies wouldn't wouldn't fit into RAM.
34
Eager filters
Filters are eager by default. A new list is created each time you use a filter.
35
Lazy filters
Sequences are data structures that use lazy evaluation, and can be used with
filters to make them lazy.
⇒ filtered: kotlin.sequences.FilteringSequence@386cc1c4
36
Sequences -> lists
Sequences can be turned back into lists using toList().
37