Upgrade to Pro — share decks privately, control downloads, hide ads and more …

5. Functional Programming [kotlin-workshop]

5. Functional Programming [kotlin-workshop]

Part of https://github.com/jetBrains/kotlin-workshop.

Covers:
- Lambda syntax
- Member references; anonymous functions
- Common operations on collections

C6997ce411091d4a51ea3caa2109c0b0?s=128

Svetlana Isakova

November 01, 2017
Tweet

Transcript

  1. Functional Programming in Kotlin

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

  3. button.addActionListener(new ActionListener() {
 @Override
 public void actionPerformed(ActionEvent e) {
 System.out.println("Hi");


    }
 }); Lambdas vs anonymous classes button.addActionListener { println("Hi") }
  4. What’s an average age of employees working in Prague? Working

    with collections in a functional style val employees: List<Employee> data class Employee( val city: City, val age: Int ) employees.filter { it.city == City.PRAGUE } .map { it.age } .average()
  5. Lambda syntax { x: Int, y: Int -> x +

    y } parameters body always in curly braces
  6. list.any({ i: Int -> i > 0 }) full syntax

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

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

    parentheses can be omitted Lambda syntax
  9. list.any { i -> i > 0 } type can

    be omitted if it’s clear from the context Lambda syntax
  10. list.any { it > 0 } it denotes an argument

    (if it’s only one) Lambda syntax
  11. Multi-line lambda list.any { println("processing $it") it > 0 }

    Last expression is the result
  12. Return from lambda?

  13. Return from function or lambda? from function marked with fun

    fun foo(list: List<String>) { list.forEach { if (it == "foo") return } ... }
  14. return from lambda list.forEach { if (it == "foo") return@forEach

    ... } list.forEach(fun (s) { if (s == "foo") return }) Explicit syntax for anonymous function
  15. class Person(val name: String, val age: Int) people.maxBy { it.age

    } Member references Class Member people.maxBy(Person::age)
  16. Function type val sum = { x: Int, y: Int

    -> x + y } val sum: (Int, Int) -> Int = { x, y -> x + y }
  17. 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]
  18. 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]
  19. Function types: under the hood () -> Boolean Function0<Boolean> (Order)

    -> Int Function1<Order, Int> (Int, Int) -> Int Function2<Int, Int, Int>
  20. package kotlin.jvm.functions /** A function that takes 0 arguments. */

    public interface Function0<out R> : Function<R> { /** Invokes the function. */ public operator fun invoke(): R } /** A function that takes 1 argument. */ public interface Function1<in P1, out R> : Function<R> { /** Invokes the function with the specified argument. */ public operator fun invoke(p1: P1): R } /** A function that takes 2 arguments. */ public interface Function2<in P1, in P2, out R> : Function<R> { /** Invokes the function with the specified arguments. */ public operator fun invoke(p1: P1, p2: P2): R } Function interfaces
  21. Kotlin library: extensions on collections • filter • map •

    reduce • count • find • any • flatMap • groupBy • …
  22. What’s an average age of employees working in Prague? Working

    with collections with Lambdas val employees: List<Employee> data class Employee( val city: City, val age: Int ) extension functions employees.filter { it.city == City.PRAGUE }.map { it.age }.average()
  23. Using Kotlin library from Java extension function function type List<Integer>

    list = CollectionsKt.listOf(1, 2, 3); boolean hasEven = CollectionsKt.any(list, new Function1<Integer, Boolean>() { public Boolean invoke(Integer i) { return i % 2 == 0; } }); System.out.println(hasEven); true
  24. Common operations on collections

  25. filter

  26. map

  27. any (all, none)

  28. find / firstOrNull

  29. count

  30. partition

  31. zip

  32. groupBy

  33. associateBy

  34. associateBy (duplicates removed!)

  35. flatMap a “abc” b c d “def” e f a

    b c d e f map flatten
  36. Copyright © 2017 https://github.com/JetBrains/kotlin-workshop