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

Svetlana Isakova

November 01, 2017
Tweet

More Decks by Svetlana Isakova

Other Decks in Programming

Transcript

  1. 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()
  2. Lambda syntax { x: Int, y: Int -> x +

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

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

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

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

    (if it’s only one) Lambda syntax
  7. Return from function or lambda? from function marked with fun

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

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

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

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

    -> Int Function1<Order, Int> (Int, Int) -> Int Function2<Int, Int, Int>
  14. 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
  15. Kotlin library: extensions on collections • filter • map •

    reduce • count • find • any • flatMap • groupBy • …
  16. 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()
  17. 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
  18. map

  19. zip