Slide 1

Slide 1 text

Functional Programming with Arrow in Kotlin @ragunathjawahar • Obvious (previously, Uncommon Bangalore)

Slide 2

Slide 2 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 3

Slide 3 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 4

Slide 4 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 5

Slide 5 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 6

Slide 6 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 7

Slide 7 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 8

Slide 8 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 9

Slide 9 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 10

Slide 10 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 11

Slide 11 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 12

Slide 12 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 13

Slide 13 text

fun findFirst(array: Array, key: Char): Int { for (i in array.indices) { if (array[i] == key) return i } return -1 }

Slide 14

Slide 14 text

fun findFirst(array: Array, key: Char): Index { for (i in array.indices) { if (array[i] == key) return i } return -1 } typealias Index = Int

Slide 15

Slide 15 text

fun findFirst(array: Array, key: Char): Index { for (i in array.indices) { if (array[i] == key) return i } return -1 } typealias Index = Int

Slide 16

Slide 16 text

fun findFirst(array: Array, key: Char): Index { for (i in array.indices) { if (array[i] == key) return i } return -1 } typealias Index = Int

Slide 17

Slide 17 text

fun findFirst(array: Array, key: Char): Index { for (i in array.indices) { if (array[i] == key) return i } return -1 } typealias Index = Int // findFirst(chars, ‘e’) != -1

Slide 18

Slide 18 text

fun findFirst(array: Array, key: Char): Index { for (i in array.indices) { if (array[i] == key) return i } return -1 } typealias Index = Int // findFirst(chars, ‘e’) != -1

Slide 19

Slide 19 text

fun findFirst(array: Array, key: Char): Index { for (i in array.indices) { if (array[i] == key) return i } return -1 } typealias Index = Int

Slide 20

Slide 20 text

fun findFirst(array: Array, key: Char): Index? { for (i in array.indices) { if (array[i] == key) return i } return null } typealias Index = Int

Slide 21

Slide 21 text

fun findFirst(array: Array, key: Char): Option { for (i in array.indices) { if (array[i] == key) return Some(i) } return None } typealias Index = Int

Slide 22

Slide 22 text

fun findFirst(array: Array, key: Char): Option { for (i in array.indices) { if (array[i] == key) return Some(i) } return None } typealias Index = Int // absence // presence

Slide 23

Slide 23 text

fun findFirst(array: Array, key: Char): Option { for (i in array.indices) { if (array[i] == key) return Some(i) } return None } typealias Index = Int

Slide 24

Slide 24 text

fun findFirst(array: Array, key: Char): Option { for (i in array.indices) { if (array[i] == key) return Some(i) } return None } typealias Index = Int

Slide 25

Slide 25 text

fun findFirst(array: Array, key: Char): Option { for (i in array.indices) { if (array[i] == key) return Some(i) } return None } typealias Index = Int

Slide 53

Slide 53 text

Core Ideas

Slide 54

Slide 54 text

Core Ideas • Purity • Referential Transparency • Immutability • Lazy Evaluation

Slide 55

Slide 55 text

Notations

Slide 56

Slide 56 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 57

Slide 57 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 58

Slide 58 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 59

Slide 59 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 60

Slide 60 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 61

Slide 61 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 62

Slide 62 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 63

Slide 63 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 64

Slide 64 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 65

Slide 65 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 66

Slide 66 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 67

Slide 67 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 68

Slide 68 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b } // (a: Int, b: Int) -> Int

Slide 69

Slide 69 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 70

Slide 70 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 71

Slide 71 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 72

Slide 72 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 73

Slide 73 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 74

Slide 74 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 75

Slide 75 text

fun add(a: Int, b: Int): Int = a + b val addRef: (Int, Int) -> Int = ::add val subtractLambda = { a: Int, b: Int -> a - b }

Slide 76

Slide 76 text

val addRef: (Int, Int) -> Int = ::add addRef.invoke(4, 5) addRef(4, 5)

Slide 77

Slide 77 text

Arrow Syntax compile "io.arrow-kt:arrow-syntax:$arrow_version"

Slide 78

Slide 78 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition

Slide 79

Slide 79 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition

Slide 80

Slide 80 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // bold(“Hello”)

Slide 81

Slide 81 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // Hello

Slide 82

Slide 82 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition

Slide 83

Slide 83 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // italic(“Hello”)

Slide 84

Slide 84 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // Hello

Slide 85

Slide 85 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition

Slide 86

Slide 86 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // boldItalic(“Hello”)

Slide 87

Slide 87 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // Hello

Slide 88

Slide 88 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition

Slide 89

Slide 89 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // italicBold(“Hello”)

Slide 90

Slide 90 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition // Hello

Slide 91

Slide 91 text

val bold: (String) -> String = { "$it" } val italic: (String) -> String = { "$it" } val boldItalic = bold compose italic val italicBold = bold forwardCompose italic Composition

Slide 92

Slide 92 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 93

Slide 93 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 94

Slide 94 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 95

Slide 95 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 96

Slide 96 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 97

Slide 97 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 98

Slide 98 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 99

Slide 99 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 100

Slide 100 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application // x2(10)

Slide 101

Slide 101 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application // 20

Slide 102

Slide 102 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 103

Slide 103 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 104

Slide 104 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 105

Slide 105 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 106

Slide 106 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 107

Slide 107 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 108

Slide 108 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application // x5(10)

Slide 109

Slide 109 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application // 50

Slide 110

Slide 110 text

fun multiply(a: Int, b: Int): Int = a * b val x2: (Int) -> Int = ::multiply.partially1(2) val x5: (Int) -> Int = ::multiply.partially1(5) Partial Application

Slide 111

Slide 111 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 112

Slide 112 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 113

Slide 113 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 114

Slide 114 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 115

Slide 115 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 116

Slide 116 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 117

Slide 117 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 118

Slide 118 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 119

Slide 119 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 120

Slide 120 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 121

Slide 121 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 122

Slide 122 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 123

Slide 123 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 124

Slide 124 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 125

Slide 125 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 126

Slide 126 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog } // findCat(animals)

Slide 127

Slide 127 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 128

Slide 128 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 129

Slide 129 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 130

Slide 130 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 131

Slide 131 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 132

Slide 132 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 133

Slide 133 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog } // findDog(animals)

Slide 134

Slide 134 text

Reverse fun findFirst( animals: List, predicate: (Animal) -> Boolean ): Option { /* … */ } val findCat: (List) -> Option = ::findFirst.reverse().partially1 { it is Cat } val findDog: (List) -> Option = ::findFirst.reverse().partially1 { it is Dog }

Slide 135

Slide 135 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 136

Slide 136 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 137

Slide 137 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 138

Slide 138 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 139

Slide 139 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 140

Slide 140 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 141

Slide 141 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 142

Slide 142 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 143

Slide 143 text

Logical Complement val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats) val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

Slide 144

Slide 144 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 145

Slide 145 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 146

Slide 146 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 147

Slide 147 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 148

Slide 148 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 149

Slide 149 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 150

Slide 150 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 151

Slide 151 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1) // 100

Slide 152

Slide 152 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 153

Slide 153 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 154

Slide 154 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 155

Slide 155 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 156

Slide 156 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 157

Slide 157 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 158

Slide 158 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 159

Slide 159 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 160

Slide 160 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1) // 100

Slide 161

Slide 161 text

Currying (1/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length volume(10, 10, 1) val curriedVolume: (width: Int) -> (height: Int) -> (length: Int) -> Int = ::volume.curried() curriedVolume(10)(10)(1)

Slide 162

Slide 162 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10)

Slide 163

Slide 163 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10)

Slide 164

Slide 164 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10)

Slide 165

Slide 165 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10) // (width, height, length)

Slide 166

Slide 166 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10) // (length, height, width)

Slide 167

Slide 167 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10) // (length) -> (height) -> (width)

Slide 168

Slide 168 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10) // (height) -> (width)

Slide 169

Slide 169 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10) // (height, width)

Slide 170

Slide 170 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10) // (width, height)

Slide 171

Slide 171 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10)

Slide 172

Slide 172 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10)

Slide 173

Slide 173 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10) // 100

Slide 174

Slide 174 text

Currying (2/2) fun volume(width: Int, height: Int, length: Int): Int = width * height * length val area = ::volume .reverse().curried()(1) .uncurried().reverse() area(10, 10)

Slide 175

Slide 175 text

More… • memoize() // FunctionN • firstOption() // Array, Sequence, and Iterable • flatten() - tail() - prependTo(List) // List • plus(C) // TupleN • paired() - tripled() // FunctionN • pipe() // FunctionN

Slide 176

Slide 176 text

Data Types compile “io.arrow-kt:arrow-core-data:$arrow_version”

Slide 177

Slide 177 text

Data Types • Option • Either • Try • Validated • Eval

Slide 178

Slide 178 text

Option sealed class Option { object None : Option() data class Some(val t: T) : Option() } Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

Slide 179

Slide 179 text

Option sealed class Option { object None : Option() data class Some(val t: T) : Option() } Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

Slide 180

Slide 180 text

Option sealed class Option { object None : Option() data class Some(val t: T) : Option() } Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

Slide 181

Slide 181 text

Option sealed class Option { object None : Option() data class Some(val t: T) : Option() } Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

Slide 182

Slide 182 text

Option sealed class Option { object None : Option() data class Some(val t: T) : Option() } Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

Slide 183

Slide 183 text

Option (Construction) val intOption: Option = Option(1) val intOption: Option = None val intOption: Option = Option.fromNullable(a) // a: Int? val intOption: Option = 1.some() val intOption: Option = none()

Slide 184

Slide 184 text

Option (Construction) val intOption: Option = Option(1) val intOption: Option = None val intOption: Option = Option.fromNullable(a) // a: Int? val intOption: Option = 1.some() val intOption: Option = none()

Slide 185

Slide 185 text

Option (Construction) val intOption: Option = Option(1) val intOption: Option = None val intOption: Option = Option.fromNullable(a) // a: Int? val intOption: Option = 1.some() val intOption: Option = none()

Slide 186

Slide 186 text

Option (Construction) val intOption: Option = Option(1) val intOption: Option = None val intOption: Option = Option.fromNullable(a) // a: Int? val intOption: Option = 1.some() val intOption: Option = none()

Slide 187

Slide 187 text

Option (Construction) val intOption: Option = Option(1) val intOption: Option = None val intOption: Option = Option.fromNullable(a) // a: Int? val intOption: Option = 1.some() val intOption: Option = none()

Slide 188

Slide 188 text

Option (Construction) val intOption: Option = Option(1) val intOption: Option = None val intOption: Option = Option.fromNullable(a) // a: Int? val intOption: Option = 1.some() val intOption: Option = none()

Slide 189

Slide 189 text

Option (Construction) val intOption: Option = Option(1) val intOption: Option = None val intOption: Option = Option.fromNullable(a) // a: Int? val intOption: Option = 1.some() val intOption: Option = none()

Slide 190

Slide 190 text

Option (Extraction) val intOption: Option = Option(1) val nullableResult: Int? = intOption.orNull() val result: Int = intOption.getOrElse { 0 }

Slide 191

Slide 191 text

Option (Extraction) val intOption: Option = Option(1) val nullableResult: Int? = intOption.orNull() val result: Int = intOption.getOrElse { 0 }

Slide 192

Slide 192 text

Option (Extraction) val intOption: Option = Option(1) val nullableResult: Int? = intOption.orNull() val result: Int = intOption.getOrElse { 0 }

Slide 193

Slide 193 text

Option (Extraction) val intOption: Option = Option(1) val nullableResult: Int? = intOption.orNull() val result: Int = intOption.getOrElse { 0 }

Slide 194

Slide 194 text

Option (Extraction) val intOption: Option = Option(1) val nullableResult: Int? = intOption.orNull() val result: Int = intOption.getOrElse { 0 }

Slide 195

Slide 195 text

Option (Extraction - when) val intOption: Option = Option(1) when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

Slide 196

Slide 196 text

Option (Extraction - when) val intOption: Option = Option(1) when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

Slide 197

Slide 197 text

Option (Extraction - when) val intOption: Option = Option(1) when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

Slide 198

Slide 198 text

Option (Extraction - when) val intOption: Option = Option(1) when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

Slide 199

Slide 199 text

Option (Extraction - when) val intOption: Option = Option(1) when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

Slide 200

Slide 200 text

Option (Extraction - when) val intOption: Option = Option(1) when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

Slide 201

Slide 201 text

Option (Extraction - when) val intOption: Option = Option(1) when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

Slide 202

Slide 202 text

Option (Extraction - fold) val intOption: Option = Option(1) intOption.fold( { 0 }, { it + 1 } ) // Some(2)

Slide 203

Slide 203 text

Option (Extraction - fold) val intOption: Option = Option(1) intOption.fold( { 0 }, { it + 1 } ) // Some(2)

Slide 204

Slide 204 text

Option (Extraction - fold) val intOption: Option = Option(1) intOption.fold( { 0 }, { it + 1 } ) // Some(2)

Slide 205

Slide 205 text

Option (Extraction - fold) val intOption: Option = Option(1) intOption.fold( { 0 }, { it + 1 } ) // Some(2)

Slide 206

Slide 206 text

Option (Extraction - fold) val intOption: Option = Option(1) intOption.fold( { 0 }, { it + 1 } ) // Some(2)

Slide 207

Slide 207 text

Option (Extraction - fold) val intOption: Option = Option(1) intOption.fold( { 0 }, { it + 1 } ) // Some(2)

Slide 208

Slide 208 text

Option (Extraction - fold) val intOption: Option = Option(1) intOption.fold( { 0 }, { it + 1 } ) // Some(2)

Slide 209

Slide 209 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 210

Slide 210 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 211

Slide 211 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 212

Slide 212 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 213

Slide 213 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 214

Slide 214 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 215

Slide 215 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 216

Slide 216 text

Option (map) val intOption: Option = Option(1) intOption.map { it * 2 } // Some(2) val intOption: Option = None intOption.map { it * 2 } // None

Slide 217

Slide 217 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = Option(2) oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

Slide 218

Slide 218 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = Option(2) oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

Slide 219

Slide 219 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = Option(2) oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

Slide 220

Slide 220 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = Option(2) oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

Slide 221

Slide 221 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = Option(2) oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

Slide 222

Slide 222 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = Option(2) oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

Slide 223

Slide 223 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = Option(2) oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

Slide 224

Slide 224 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = None oneOption.flatMap { one -> twoOption.map { two -> one + two } } // None

Slide 225

Slide 225 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = None oneOption.flatMap { one -> twoOption.map { two -> one + two } } // None

Slide 226

Slide 226 text

Option (flatMap) val oneOption: Option = Option(1) val twoOption: Option = None oneOption.flatMap { one -> twoOption.map { two -> one + two } } // None

Slide 227

Slide 227 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 228

Slide 228 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 229

Slide 229 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 230

Slide 230 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 231

Slide 231 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 232

Slide 232 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 233

Slide 233 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 234

Slide 234 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 235

Slide 235 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = Option(2) Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // Some(3)

Slide 236

Slide 236 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = None Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // None

Slide 237

Slide 237 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = None Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // None

Slide 238

Slide 238 text

Option (Monad Binding) val oneOption: Option = Option(1) val twoOption: Option = None Option.monad().binding { val one = oneOption.bind() val two = twoOption.bind() one + two }.fix() // None

Slide 239

Slide 239 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 240

Slide 240 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 241

Slide 241 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 242

Slide 242 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 243

Slide 243 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 244

Slide 244 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 245

Slide 245 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 246

Slide 246 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 247

Slide 247 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 248

Slide 248 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 249

Slide 249 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = Option(Foam(0.3)) Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // Some(Cappuccino(espresso=Espresso(%=0.3), milk=Milk(%=0.3), // foam=Foam(%=0.3)))

Slide 250

Slide 250 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = None Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // None

Slide 251

Slide 251 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = None Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // None

Slide 252

Slide 252 text

Option (Applicative Builder) val maybeEspresso: Option = Option(Espresso(0.3)) val maybeMilkOption: Option = Option(Milk(0.3)) val foamOption: Option = None Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix() // None

Slide 253

Slide 253 text

Either sealed class Either { data class Left(val a: A) : Either() data class Right(val b: B) : Either() } Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

Slide 254

Slide 254 text

Either sealed class Either { data class Left(val a: A) : Either() data class Right(val b: B) : Either() } Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

Slide 255

Slide 255 text

Either sealed class Either { data class Left(val a: A) : Either() data class Right(val b: B) : Either() } Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

Slide 256

Slide 256 text

Either sealed class Either { data class Left(val a: A) : Either() data class Right(val b: B) : Either() } Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

Slide 257

Slide 257 text

Either sealed class Either { data class Left(val a: A) : Either() data class Right(val b: B) : Either() } Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

Slide 258

Slide 258 text

Either (Construction) sealed class DomainError { /* ... */ } val result: Either = Right(1) val result: Either = Left(UnknownUser) val result: Either = 1.right() val result: Either = UnknownUser.left()

Slide 259

Slide 259 text

Either (Construction) sealed class DomainError { /* ... */ } val result: Either = Right(1) val result: Either = Left(UnknownUser) val result: Either = 1.right() val result: Either = UnknownUser.left()

Slide 260

Slide 260 text

Either (Construction) sealed class DomainError { /* ... */ } val result: Either = Right(1) val result: Either = Left(UnknownUser) val result: Either = 1.right() val result: Either = UnknownUser.left()

Slide 261

Slide 261 text

Either (Construction) sealed class DomainError { /* ... */ } val result: Either = Right(1) val result: Either = Left(UnknownUser) val result: Either = 1.right() val result: Either = UnknownUser.left()

Slide 262

Slide 262 text

Either (Construction) sealed class DomainError { /* ... */ } val result: Either = Right(1) val result: Either = Left(UnknownUser) val result: Either = 1.right() val result: Either = UnknownUser.left()

Slide 263

Slide 263 text

Either (Construction) sealed class DomainError { /* ... */ } val result: Either = Right(1) val result: Either = Left(UnknownUser) val result: Either = 1.right() val result: Either = UnknownUser.left()

Slide 264

Slide 264 text

Either (Construction) sealed class DomainError { /* ... */ } val result: Either = Right(1) val result: Either = Left(UnknownUser) val result: Either = 1.right() val result: Either = UnknownUser.left()

Slide 265

Slide 265 text

Either (Extraction - when) sealed class DomainError { /* ... */ } val result: Either = Right(1) when (result) { is Left -> 0 is Right -> result.b } // Right(1)

Slide 266

Slide 266 text

Either (Extraction - when) sealed class DomainError { /* ... */ } val result: Either = Right(1) when (result) { is Left -> 0 is Right -> result.b } // Right(1)

Slide 267

Slide 267 text

Either (Extraction - when) sealed class DomainError { /* ... */ } val result: Either = Right(1) when (result) { is Left -> 0 is Right -> result.b } // Right(1)

Slide 268

Slide 268 text

Either (Extraction - when) sealed class DomainError { /* ... */ } val result: Either = Right(1) when (result) { is Left -> 0 is Right -> result.b } // Right(1)

Slide 269

Slide 269 text

Either (Extraction - when) sealed class DomainError { /* ... */ } val result: Either = Right(1) when (result) { is Left -> 0 is Right -> result.b } // Right(1) F

Slide 270

Slide 270 text

Either (Extraction - when) sealed class DomainError { /* ... */ } val result: Either = Right(1) when (result) { is Left -> 0 is Right -> result.b } // Right(1)

Slide 271

Slide 271 text

Either (Extraction - when) sealed class DomainError { /* ... */ } val result: Either = Right(1) when (result) { is Left -> 0 is Right -> result.b } // Right(1)

Slide 272

Slide 272 text

Either (Extraction - fold) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.fold( { 0 }, { it + 1 } ) // Right(2)

Slide 273

Slide 273 text

Either (Extraction - fold) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.fold( { 0 }, { it + 1 } ) // Right(2)

Slide 274

Slide 274 text

Either (Extraction - fold) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.fold( { 0 }, { it + 1 } ) // Right(2)

Slide 275

Slide 275 text

Either (Extraction - fold) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.fold( { 0 }, { it + 1 } ) // Right(2)

Slide 276

Slide 276 text

Either (Extraction - fold) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.fold( { 0 }, { it + 1 } ) // Right(2)

Slide 277

Slide 277 text

Either (Extraction - fold) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.fold( { 0 }, { it + 1 } ) // Right(2)

Slide 278

Slide 278 text

Either (Extraction - fold) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.fold( { 0 }, { it + 1 } ) // Right(2)

Slide 279

Slide 279 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 280

Slide 280 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 281

Slide 281 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 282

Slide 282 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 283

Slide 283 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 284

Slide 284 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 285

Slide 285 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 286

Slide 286 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 287

Slide 287 text

Either (map) sealed class DomainError { /* ... */ } val result: Either = Right(1) result.map { it * 2 } // Right(2) val result: Either = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

Slide 288

Slide 288 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

Slide 289

Slide 289 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

Slide 290

Slide 290 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

Slide 291

Slide 291 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

Slide 292

Slide 292 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

Slide 293

Slide 293 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

Slide 294

Slide 294 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

Slide 295

Slide 295 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Left(UnknownUser) result1.flatMap { one -> result2.map { two -> one + two } } // Left(UnknownUser)

Slide 296

Slide 296 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Left(UnknownUser) result1.flatMap { one -> result2.map { two -> one + two } } // Left(UnknownUser)

Slide 297

Slide 297 text

Either (flatMap) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Left(UnknownUser) result1.flatMap { one -> result2.map { two -> one + two } } // Left(UnknownUser)

Slide 298

Slide 298 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 299

Slide 299 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 300

Slide 300 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 301

Slide 301 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 302

Slide 302 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 303

Slide 303 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 304

Slide 304 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 305

Slide 305 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 306

Slide 306 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Right(2) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

Slide 307

Slide 307 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Left(UnknownUser) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Left(UnknownUser)

Slide 308

Slide 308 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Left(UnknownUser) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Left(UnknownUser)

Slide 309

Slide 309 text

Either (Monad Binding) sealed class DomainError { /* ... */ } val result1: Either = Right(1) val result2: Either = Left(UnknownUser) Either.monad.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Left(UnknownUser)

Slide 310

Slide 310 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 311

Slide 311 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 312

Slide 312 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 313

Slide 313 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 314

Slide 314 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 315

Slide 315 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 316

Slide 316 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 317

Slide 317 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 318

Slide 318 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 319

Slide 319 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 320

Slide 320 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Right(Water(0.6)) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

Slide 321

Slide 321 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Left(RefillWater) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Left(RefillWater)

Slide 322

Slide 322 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Left(RefillWater) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Left(RefillWater)

Slide 323

Slide 323 text

Either (Applicative Builder) sealed class CoffeeMakerError { /* ... */ } val espressoResult: Either = Right(Espresso(0.4)) val waterResult: Either = Left(RefillWater) Either.applicative() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix() // Left(RefillWater)

Slide 324

Slide 324 text

More… • Try // Success - Failure • Validated // Valid - Invalid • Eval // Now - Always - Later - Defer • TupleN // Tuple2 - Tuple22

Slide 325

Slide 325 text

Typeclasses compile “io.arrow-kt:arrow-typeclasses:$arrow_version” kapt “io.arrow-kt:arrow-meta:$arrow_version”

Slide 326

Slide 326 text

Typeclasses • Polymorphism in functional programming. • Separates data from behaviour. • Maybe governed by algebraic / category theoretic laws. • Implemented as interfaces in Kotlin. • One Typeclass instance per type.

Slide 327

Slide 327 text

Typeclasses • Show • Eq • Semigroup • Functor • Monad • …

Slide 328

Slide 328 text

Show interface Show { fun A.show(): String } The Show typeclass abstracts the ability to obtain a String representation of any object.

Slide 329

Slide 329 text

Show interface Show { fun A.show(): String } The Show typeclass abstracts the ability to obtain a String representation of any object.

Slide 330

Slide 330 text

Show interface Show { fun A.show(): String } The Show typeclass abstracts the ability to obtain a String representation of any object.

Slide 331

Slide 331 text

Show interface Show { fun A.show(): String } The Show typeclass abstracts the ability to obtain a String representation of any object.

Slide 332

Slide 332 text

Eq interface Eq { fun F.eqv(b: F): Boolean fun F.neqv(b: F): Boolean = !eqv(b) } The Eq typeclass abstracts the ability to compare two instances of any object. It can be compared to the typeclass equivalent of Java’s Object#equals.

Slide 333

Slide 333 text

Eq interface Eq { fun F.eqv(b: F): Boolean fun F.neqv(b: F): Boolean = !eqv(b) } The Eq typeclass abstracts the ability to compare two instances of any object. It can be compared to the typeclass equivalent of Java’s Object#equals.

Slide 334

Slide 334 text

Eq interface Eq { fun F.eqv(b: F): Boolean fun F.neqv(b: F): Boolean = !eqv(b) } The Eq typeclass abstracts the ability to compare two instances of any object. It can be compared to the typeclass equivalent of Java’s Object#equals.

Slide 335

Slide 335 text

Eq interface Eq { fun F.eqv(b: F): Boolean fun F.neqv(b: F): Boolean = !eqv(b) } The Eq typeclass abstracts the ability to compare two instances of any object. It can be compared to the typeclass equivalent of Java’s Object#equals.

Slide 336

Slide 336 text

Eq interface Eq { fun F.eqv(b: F): Boolean fun F.neqv(b: F): Boolean = !eqv(b) } The Eq typeclass abstracts the ability to compare two instances of any object. It can be compared to the typeclass equivalent of Java’s Object#equals.

Slide 342

Slide 342 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 343

Slide 343 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 344

Slide 344 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 345

Slide 345 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 346

Slide 346 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 347

Slide 347 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 348

Slide 348 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 349

Slide 349 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 350

Slide 350 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 351

Slide 351 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 352

Slide 352 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 353

Slide 353 text

Creating Instances data class Person(val firstName: String, val lastName: String) { companion object } @extension interface PersonShow : Show { override fun Person.show(): String = "$firstName $lastName" } val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

Slide 354

Slide 354 text

More… • Hash • Order • Traverse • Applicative • And many more…

Slide 355

Slide 355 text

Optics compile “io.arrow-kt:arrow-optics:$arrow_version” kapt “io.arrow-kt:arrow-meta:$arrow_version”

Slide 356

Slide 356 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 357

Slide 357 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 358

Slide 358 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 359

Slide 359 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 360

Slide 360 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 361

Slide 361 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 362

Slide 362 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 363

Slide 363 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 364

Slide 364 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 365

Slide 365 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 366

Slide 366 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 367

Slide 367 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 368

Slide 368 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 369

Slide 369 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 370

Slide 370 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 371

Slide 371 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 372

Slide 372 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 373

Slide 373 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 374

Slide 374 text

Lens @optics data class UserProfile( val name: Name, /* Not shown for brevity */ val contact: Contact ) { companion object } @optics data class Contact( val email: String, val phone: String ) { companion object }

Slide 375

Slide 375 text

Without Lens val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") ) val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

Slide 376

Slide 376 text

Without Lens val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") ) val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

Slide 377

Slide 377 text

Without Lens val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") ) val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

Slide 378

Slide 378 text

Without Lens val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") ) val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

Slide 379

Slide 379 text

Without Lens val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") ) val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

Slide 380

Slide 380 text

Without Lens val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") ) val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

Slide 381

Slide 381 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 382

Slide 382 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 383

Slide 383 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 384

Slide 384 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 385

Slide 385 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 386

Slide 386 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 387

Slide 387 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 388

Slide 388 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 389

Slide 389 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 390

Slide 390 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 391

Slide 391 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 392

Slide 392 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 393

Slide 393 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 394

Slide 394 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 395

Slide 395 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 396

Slide 396 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile) // “[email protected]

Slide 397

Slide 397 text

With Lens val profile = UserProfile( /* … */ ) val emailLens: Lens = UserProfile.contact.email val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail } val email = emailLens.get(profile)

Slide 398

Slide 398 text

Optional @optics data class ShoppingCart( val id: String, val cartItems: ListK ) { companion object } @optics data class CartItem( val product: Product, /* Not shown for brevity */ val quantity: Int ) { companion object }

Slide 399

Slide 399 text

Optional @optics data class ShoppingCart( val id: String, val cartItems: ListK ) { companion object } @optics data class CartItem( val product: Product, /* Not shown for brevity */ val quantity: Int ) { companion object }

Slide 400

Slide 400 text

Optional @optics data class ShoppingCart( val id: String, val cartItems: ListK ) { companion object } @optics data class CartItem( val product: Product, /* Not shown for brevity */ val quantity: Int ) { companion object }

Slide 401

Slide 401 text

Optional @optics data class ShoppingCart( val id: String, val cartItems: ListK ) { companion object } @optics data class CartItem( val product: Product, /* Not shown for brevity */ val quantity: Int ) { companion object }

Slide 402

Slide 402 text

Optional @optics data class ShoppingCart( val id: String, val cartItems: ListK ) { companion object } @optics data class CartItem( val product: Product, /* Not shown for brevity */ val quantity: Int ) { companion object }

Slide 403

Slide 403 text

Optional @optics data class ShoppingCart( val id: String, val cartItems: ListK ) { companion object } @optics data class CartItem( val product: Product, /* Not shown for brevity */ val quantity: Int ) { companion object }

Slide 404

Slide 404 text

Optional @optics data class ShoppingCart( val id: String, val cartItems: ListK ) { companion object } @optics data class CartItem( val product: Product, /* Not shown for brevity */ val quantity: Int ) { companion object }

Slide 405

Slide 405 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 406

Slide 406 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 407

Slide 407 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 408

Slide 408 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 409

Slide 409 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 410

Slide 410 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 411

Slide 411 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 412

Slide 412 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 413

Slide 413 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 414

Slide 414 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 415

Slide 415 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 416

Slide 416 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 417

Slide 417 text

Optional (Set) val shoppingCart = ShoppingCart("CRT19321", cartItems) val quantityOptional: Optional = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

Slide 418

Slide 418 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 419

Slide 419 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 420

Slide 420 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 421

Slide 421 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 422

Slide 422 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 423

Slide 423 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 424

Slide 424 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 425

Slide 425 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 426

Slide 426 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 427

Slide 427 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 428

Slide 428 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 429

Slide 429 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 430

Slide 430 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 431

Slide 431 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 432

Slide 432 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 433

Slide 433 text

Optional (Get) val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart) val sameCartItemOption = ListK.index().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

Slide 434

Slide 434 text

More… • Iso • Prism • Getter / Setter • Fold • Each • And others …

Slide 435

Slide 435 text

That’s not it… • Arrow Fx • Effects • Arrow Query Language • Generic • Integrations • Free • Recursion Schemes

Slide 436

Slide 436 text

Resources • https://arrow-kt.io/docs/ • http://learnyouahaskell.com/ • https://caster.io/courses/functional-programming-in-kotlin-with-arrow • https://github.com/hemanth/functional-programming-jargon#function • https://wiki.haskell.org/Typeclassopedia • http://nealford.com/functionalthinking.html • https://prod.packtpub.com/in/application-development/functional-kotlin

Slide 437

Slide 437 text

end; @ragunathjawahar Twitter / Medium / GitHub