Slide 1

Slide 1 text

Functional Programming in Kotlin

Slide 2

Slide 2 text

Lambdas button.addActionListener { println("Hi") }

Slide 3

Slide 3 text

button.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 System.out.println("Hi");
 }
 }); Lambdas vs anonymous classes button.addActionListener { println("Hi") }

Slide 4

Slide 4 text

What’s an average age of employees working in Prague? Working with collections in a functional style val employees: List data class Employee( val city: City, val age: Int ) employees.filter { it.city == City.PRAGUE } .map { it.age } .average()

Slide 5

Slide 5 text

Lambda syntax { x: Int, y: Int -> x + y } parameters body always in curly braces

Slide 6

Slide 6 text

list.any({ i: Int -> i > 0 }) full syntax Lambda syntax

Slide 7

Slide 7 text

list.any() { i: Int -> i > 0 } when lambda is the last argument, it can be moved out of parentheses Lambda syntax

Slide 8

Slide 8 text

list.any { i: Int -> i > 0 } empty parentheses can be omitted Lambda syntax

Slide 9

Slide 9 text

list.any { i -> i > 0 } type can be omitted if it’s clear from the context Lambda syntax

Slide 10

Slide 10 text

list.any { it > 0 } it denotes an argument (if it’s only one) Lambda syntax

Slide 11

Slide 11 text

Multi-line lambda list.any { println("processing $it") it > 0 } Last expression is the result

Slide 12

Slide 12 text

Return from lambda?

Slide 13

Slide 13 text

Return from function or lambda? from function marked with fun fun foo(list: List) { list.forEach { if (it == "foo") return } ... }

Slide 14

Slide 14 text

return from lambda list.forEach { if (it == "foo") return@forEach ... } list.forEach(fun (s) { if (s == "foo") return }) Explicit syntax for anonymous function

Slide 15

Slide 15 text

class Person(val name: String, val age: Int) people.maxBy { it.age } Member references Class Member people.maxBy(Person::age)

Slide 16

Slide 16 text

Function type val sum = { x: Int, y: Int -> x + y } val sum: (Int, Int) -> Int = { x, y -> x + y }

Slide 17

Slide 17 text

Storing lambda in a variable val isEven: (Int) -> Boolean = { i: Int -> i % 2 == 0 } val list = listOf(1, 2, 3, 4) list.any(isEven) list.filter(isEven) true [2, 4]

Slide 18

Slide 18 text

Passing member reference as argument fun isEven(i: Int): Boolean = i % 2 == 0 val list = listOf(1, 2, 3, 4) list.any(::isEven) list.filter(::isEven) true [2, 4]

Slide 19

Slide 19 text

Function types: under the hood () -> Boolean Function0 (Order) -> Int Function1 (Int, Int) -> Int Function2

Slide 20

Slide 20 text

package kotlin.jvm.functions /** A function that takes 0 arguments. */ public interface Function0 : Function { /** Invokes the function. */ public operator fun invoke(): R } /** A function that takes 1 argument. */ public interface Function1 : Function { /** Invokes the function with the specified argument. */ public operator fun invoke(p1: P1): R } /** A function that takes 2 arguments. */ public interface Function2 : Function { /** Invokes the function with the specified arguments. */ public operator fun invoke(p1: P1, p2: P2): R } Function interfaces

Slide 21

Slide 21 text

Kotlin library: extensions on collections • filter • map • reduce • count • find • any • flatMap • groupBy • …

Slide 22

Slide 22 text

What’s an average age of employees working in Prague? Working with collections with Lambdas val employees: List data class Employee( val city: City, val age: Int ) extension functions employees.filter { it.city == City.PRAGUE }.map { it.age }.average()

Slide 23

Slide 23 text

Using Kotlin library from Java extension function function type List list = CollectionsKt.listOf(1, 2, 3); boolean hasEven = CollectionsKt.any(list, new Function1() { public Boolean invoke(Integer i) { return i % 2 == 0; } }); System.out.println(hasEven); true

Slide 24

Slide 24 text

Common operations on collections

Slide 25

Slide 25 text

filter

Slide 26

Slide 26 text

map

Slide 27

Slide 27 text

any (all, none)

Slide 28

Slide 28 text

find / firstOrNull

Slide 29

Slide 29 text

count

Slide 30

Slide 30 text

partition

Slide 31

Slide 31 text

zip

Slide 32

Slide 32 text

groupBy

Slide 33

Slide 33 text

associateBy

Slide 34

Slide 34 text

associateBy (duplicates removed!)

Slide 35

Slide 35 text

flatMap a “abc” b c d “def” e f a b c d e f map flatten

Slide 36

Slide 36 text

Copyright © 2017 https://github.com/JetBrains/kotlin-workshop