439

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

  • Upload
    others

  • View
    39

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Functional Programming with Arrow in Kotlin · Functional Programming with Arrow in Kotlin @ragunathjawahar • Obvious (previously, Uncommon Bangalore)
Page 2: Functional Programming with Arrow in Kotlin · Functional Programming with Arrow in Kotlin @ragunathjawahar • Obvious (previously, Uncommon Bangalore)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

// findFirst(chars, ‘e’) != -1

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

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

typealias Index = Int

// findFirst(chars, ‘e’) != -1

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

// absence

// presence

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

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

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

typealias Index = Int

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

fun <A> findFirst(array: Array<A>, p: (A) -> Boolean): Option<Index> { for (i in array.indices) { if (p(array[i])) return Some(i) } return None }

typealias Index = Int

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

fun <A> findFirst(array: Array<A>, p: (A) -> Boolean): Option<Index> { for (i in array.indices) { if (p(array[i])) return Some(i) } return None }

typealias Index = Int

// findFirst(people, { it.firstName == “Ajay” })

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

fun <A> findFirst(array: Array<A>, p: (A) -> Boolean): Option<Index> { for (i in array.indices) { if (p(array[i])) return Some(i) } return None }

typealias Index = Int

// findFirst(people) { it.firstName == “Ajay” }

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

fun <A> findFirst(array: Array<A>, p: (A) -> Boolean): Option<Index> { for (i in array.indices) { if (p(array[i])) return Some(i) } return None }

typealias Index = Int

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

fun <A> findFirst(array: Array<A>, p: (A) -> Boolean): Option<Index> { for (i in array.indices) { if (p(array[i])) return Some(i) } return None }

typealias Index = Int

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

fun <A> findFirst(array: Array<A>, p: (A) -> Boolean): Option<Index> { for (i in array.indices) { if (p(array[i])) return Some(i) } return None }

typealias Index = Int

// findFirst(people) { it.firstName == “Ajay” }

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

fun <A> Array<A>.findFirst(p:,(A) -> Boolean): Option<Index> { for (i in this.indices) { if (p(this[i])) return Some(i) } return None }

typealias Index = Int

// people.findFirst { it.firstName == “Ajay” }

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

fun <A> Array<A>.findFirst(p:,(A) -> Boolean): Option<Index> { for (i in this.indices) { if (p(this[i])) return Some(i) } return None }

typealias Index = Int

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

fun <A> Array<A>.findFirst(p:,(A) -> Boolean): Option<Index> { for (i in this.indices) { if (p(this[i])) return Some(i) } return None }

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

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

fun <A> findFirst(array: Array<A>, p:,(A) -> Boolean): Option<Index> { tailrec fun go(array: Array<A>, index: Index): Option<Index> { return when { index == array.size -> None p(array[index]) -> Some(index) else -> go(array, index + 1) } }

return go(array, 0) }

But!

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

Core Ideas

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

Core Ideas

• Purity

• Referential Transparency

• Immutability

• Lazy Evaluation

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

Notations

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

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

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

fun add(a: Int, b: Int): Int = a + b

val addRef: (Int, Int) -> Int = ::add

val subtractLambda = { a: Int, b: Int -> a - b }

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

val addRef: (Int, Int) -> Int = ::add

addRef.invoke(4, 5)

addRef(4, 5)

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

Arrow Syntaxcompile "io.arrow-kt:arrow-syntax:$arrow_version"

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// bold(“Hello”)

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// <b>Hello</b>

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// italic(“Hello”)

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// <i>Hello</i>

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// boldItalic(“Hello”)

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// <b><i>Hello</i></b>

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// italicBold(“Hello”)

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

// <i><b>Hello</b></i>

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

val bold: (String) -> String = { "<b>$it</b>" }

val italic: (String) -> String = { "<i>$it</i>" }

val boldItalic = bold compose italic

val italicBold = bold forwardCompose italic

Composition

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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)

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

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

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

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

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

// findCat(animals)

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog } // findDog(animals)

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

Reverse

fun findFirst( animals: List<Animal>, predicate: (Animal) -> Boolean ): Option<Animal> { /* … */ }

val findCat: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Cat }

val findDog: (List<Animal>) -> Option<Animal> = ::findFirst.reverse().partially1 { it is Dog }

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

Logical Complement

val onlyCats: (Animal) -> Boolean = { it is Cat } val allCats = filter(animals, onlyCats)

val noCats = onlyCats.complement() val allExceptCats = filter(animals, noCats)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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)

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

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

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

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)

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

More…

• memoize() // FunctionN

• firstOption() // Array, Sequence, and Iterable

• flatten() - tail() - prependTo(List<T>) // List

• plus(C) // TupleN

• paired() - tripled() // FunctionN

• pipe() // FunctionN

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

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

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

Data Types

• Option

• Either

• Try

• Validated

• Eval

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

Option

sealed class Option<out A> { object None : Option<Nothing>() data class Some<out T>(val t: T) : Option<T>() }

Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

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

Option

sealed class Option<out A> { object None : Option<Nothing>() data class Some<out T>(val t: T) : Option<T>() }

Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

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

Option

sealed class Option<out A> { object None : Option<Nothing>() data class Some<out T>(val t: T) : Option<T>() }

Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

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

Option

sealed class Option<out A> { object None : Option<Nothing>() data class Some<out T>(val t: T) : Option<T>() }

Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

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

Option

sealed class Option<out A> { object None : Option<Nothing>() data class Some<out T>(val t: T) : Option<T>() }

Option is an Algebraic Data Type that is used to model the absence of value. In Kotlin they are encoded with sealed class hierarchies.

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

Option (Construction)

val intOption: Option<Int> = Option(1)

val intOption: Option<Int> = None

val intOption: Option<Int> = Option.fromNullable(a) // a: Int?

val intOption: Option<Int> = 1.some()

val intOption: Option<Int> = none()

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

Option (Construction)

val intOption: Option<Int> = Option(1)

val intOption: Option<Int> = None

val intOption: Option<Int> = Option.fromNullable(a) // a: Int?

val intOption: Option<Int> = 1.some()

val intOption: Option<Int> = none()

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

Option (Construction)

val intOption: Option<Int> = Option(1)

val intOption: Option<Int> = None

val intOption: Option<Int> = Option.fromNullable(a) // a: Int?

val intOption: Option<Int> = 1.some()

val intOption: Option<Int> = none()

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

Option (Construction)

val intOption: Option<Int> = Option(1)

val intOption: Option<Int> = None

val intOption: Option<Int> = Option.fromNullable(a) // a: Int?

val intOption: Option<Int> = 1.some()

val intOption: Option<Int> = none()

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

Option (Construction)

val intOption: Option<Int> = Option(1)

val intOption: Option<Int> = None

val intOption: Option<Int> = Option.fromNullable(a) // a: Int?

val intOption: Option<Int> = 1.some()

val intOption: Option<Int> = none()

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

Option (Construction)

val intOption: Option<Int> = Option(1)

val intOption: Option<Int> = None

val intOption: Option<Int> = Option.fromNullable(a) // a: Int?

val intOption: Option<Int> = 1.some()

val intOption: Option<Int> = none()

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

Option (Construction)

val intOption: Option<Int> = Option(1)

val intOption: Option<Int> = None

val intOption: Option<Int> = Option.fromNullable(a) // a: Int?

val intOption: Option<Int> = 1.some()

val intOption: Option<Int> = none()

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

Option (Extraction)

val intOption: Option<Int> = Option(1)

val nullableResult: Int? = intOption.orNull()

val result: Int = intOption.getOrElse { 0 }

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

Option (Extraction)

val intOption: Option<Int> = Option(1)

val nullableResult: Int? = intOption.orNull()

val result: Int = intOption.getOrElse { 0 }

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

Option (Extraction)

val intOption: Option<Int> = Option(1)

val nullableResult: Int? = intOption.orNull()

val result: Int = intOption.getOrElse { 0 }

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

Option (Extraction)

val intOption: Option<Int> = Option(1)

val nullableResult: Int? = intOption.orNull()

val result: Int = intOption.getOrElse { 0 }

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

Option (Extraction)

val intOption: Option<Int> = Option(1)

val nullableResult: Int? = intOption.orNull()

val result: Int = intOption.getOrElse { 0 }

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

Option (Extraction - when)

val intOption: Option<Int> = Option(1)

when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

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

Option (Extraction - when)

val intOption: Option<Int> = Option(1)

when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

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

Option (Extraction - when)

val intOption: Option<Int> = Option(1)

when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

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

Option (Extraction - when)

val intOption: Option<Int> = Option(1)

when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

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

Option (Extraction - when)

val intOption: Option<Int> = Option(1)

when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

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

Option (Extraction - when)

val intOption: Option<Int> = Option(1)

when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

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

Option (Extraction - when)

val intOption: Option<Int> = Option(1)

when (intOption) { is None -> 0 is Some -> intOption.t } // Some(1)

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

Option (Extraction - fold)

val intOption: Option<Int> = Option(1)

intOption.fold( { 0 }, { it + 1 } ) // Some(2)

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

Option (Extraction - fold)

val intOption: Option<Int> = Option(1)

intOption.fold( { 0 }, { it + 1 } ) // Some(2)

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

Option (Extraction - fold)

val intOption: Option<Int> = Option(1)

intOption.fold( { 0 }, { it + 1 } ) // Some(2)

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

Option (Extraction - fold)

val intOption: Option<Int> = Option(1)

intOption.fold( { 0 }, { it + 1 } ) // Some(2)

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

Option (Extraction - fold)

val intOption: Option<Int> = Option(1)

intOption.fold( { 0 }, { it + 1 } ) // Some(2)

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

Option (Extraction - fold)

val intOption: Option<Int> = Option(1)

intOption.fold( { 0 }, { it + 1 } ) // Some(2)

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

Option (Extraction - fold)

val intOption: Option<Int> = Option(1)

intOption.fold( { 0 }, { it + 1 } ) // Some(2)

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (map)

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

val intOption: Option<Int> = None intOption.map { it * 2 } // None

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // Some(3)

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = None

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // None

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = None

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // None

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

Option (flatMap)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = None

oneOption.flatMap { one -> twoOption.map { two -> one + two } } // None

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = Option(2)

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = None

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = None

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

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

Option (Monad Binding)

val oneOption: Option<Int> = Option(1) val twoOption: Option<Int> = None

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

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = 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)))

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = None

Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix()

// None

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = None

Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix()

// None

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

Option (Applicative Builder)

val maybeEspresso: Option<Espresso> = Option(Espresso(0.3)) val maybeMilkOption: Option<Milk> = Option(Milk(0.3)) val foamOption: Option<Foam> = None

Option.applicative() .map(maybeEspresso, maybeMilk, maybeFoam) { (espresso, milk, foam) -> Cappuccino(espresso, milk, foam) }.fix()

// None

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

Either

sealed class Either<out A, out B> { data class Left<out A, out B>(val a: A) : Either<A, B>() data class Right<out A, out B>(val b: B) : Either<A, B>() }

Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

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

Either

sealed class Either<out A, out B> { data class Left<out A, out B>(val a: A) : Either<A, B>() data class Right<out A, out B>(val b: B) : Either<A, B>() }

Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

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

Either

sealed class Either<out A, out B> { data class Left<out A, out B>(val a: A) : Either<A, B>() data class Right<out A, out B>(val b: B) : Either<A, B>() }

Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

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

Either

sealed class Either<out A, out B> { data class Left<out A, out B>(val a: A) : Either<A, B>() data class Right<out A, out B>(val b: B) : Either<A, B>() }

Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

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

Either

sealed class Either<out A, out B> { data class Left<out A, out B>(val a: A) : Either<A, B>() data class Right<out A, out B>(val b: B) : Either<A, B>() }

Either is an Algebraic Data Type that is used to model a return type that may return one of two possible values.

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

Either (Construction)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

val result: Either<DomainError, Int> = Left(UnknownUser)

val result: Either<DomainError, Int> = 1.right()

val result: Either<DomainError, Int> = UnknownUser.left()

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

Either (Construction)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

val result: Either<DomainError, Int> = Left(UnknownUser)

val result: Either<DomainError, Int> = 1.right()

val result: Either<DomainError, Int> = UnknownUser.left()

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

Either (Construction)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

val result: Either<DomainError, Int> = Left(UnknownUser)

val result: Either<DomainError, Int> = 1.right()

val result: Either<DomainError, Int> = UnknownUser.left()

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

Either (Construction)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

val result: Either<DomainError, Int> = Left(UnknownUser)

val result: Either<DomainError, Int> = 1.right()

val result: Either<DomainError, Int> = UnknownUser.left()

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

Either (Construction)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

val result: Either<DomainError, Int> = Left(UnknownUser)

val result: Either<DomainError, Int> = 1.right()

val result: Either<DomainError, Int> = UnknownUser.left()

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

Either (Construction)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

val result: Either<DomainError, Int> = Left(UnknownUser)

val result: Either<DomainError, Int> = 1.right()

val result: Either<DomainError, Int> = UnknownUser.left()

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

Either (Construction)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

val result: Either<DomainError, Int> = Left(UnknownUser)

val result: Either<DomainError, Int> = 1.right()

val result: Either<DomainError, Int> = UnknownUser.left()

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

Either (Extraction - when)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

when (result) { is Left -> 0 is Right -> result.b } // Right(1)

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

Either (Extraction - when)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

when (result) { is Left -> 0 is Right -> result.b } // Right(1)

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

Either (Extraction - when)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

when (result) { is Left -> 0 is Right -> result.b } // Right(1)

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

Either (Extraction - when)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

when (result) { is Left -> 0 is Right -> result.b } // Right(1)

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

Either (Extraction - when)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

when (result) { is Left -> 0 is Right -> result.b } // Right(1) F

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

Either (Extraction - when)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

when (result) { is Left -> 0 is Right -> result.b } // Right(1)

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

Either (Extraction - when)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

when (result) { is Left -> 0 is Right -> result.b } // Right(1)

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

Either (Extraction - fold)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

result.fold( { 0 }, { it + 1 } ) // Right(2)

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

Either (Extraction - fold)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

result.fold( { 0 }, { it + 1 } ) // Right(2)

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

Either (Extraction - fold)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

result.fold( { 0 }, { it + 1 } ) // Right(2)

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

Either (Extraction - fold)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

result.fold( { 0 }, { it + 1 } ) // Right(2)

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

Either (Extraction - fold)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

result.fold( { 0 }, { it + 1 } ) // Right(2)

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

Either (Extraction - fold)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

result.fold( { 0 }, { it + 1 } ) // Right(2)

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

Either (Extraction - fold)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1)

result.fold( { 0 }, { it + 1 } ) // Right(2)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (map)

sealed class DomainError { /* ... */ }

val result: Either<DomainError, Int> = Right(1) result.map { it * 2 } // Right(2)

val result: Either<DomainError, Int> = Left(UnknownUser) result.map { it * 2 } // Left(UnknownUser)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

result1.flatMap { one -> result2.map { two -> one + two } } // Right(3)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Left(UnknownUser)

result1.flatMap { one -> result2.map { two -> one + two } } // Left(UnknownUser)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Left(UnknownUser)

result1.flatMap { one -> result2.map { two -> one + two } } // Left(UnknownUser)

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

Either (flatMap)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Left(UnknownUser)

result1.flatMap { one -> result2.map { two -> one + two } } // Left(UnknownUser)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Right(2)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Right(3)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Left(UnknownUser)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Left(UnknownUser)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Left(UnknownUser)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Left(UnknownUser)

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

Either (Monad Binding)

sealed class DomainError { /* ... */ }

val result1: Either<DomainError, Int> = Right(1) val result2: Either<DomainError, Int> = Left(UnknownUser)

Either.monad<DomainError>.binding { val one = result1.bind() val two = result2.bind() one + two }.fix() // Left(UnknownUser)

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Right(Water(0.6))

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Right(Americano(espresso=Espresso(%=0.4), water=Water(%=0.6)))

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Left(RefillWater)

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Left(RefillWater)

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Left(RefillWater)

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Left(RefillWater)

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

Either (Applicative Builder)sealed class CoffeeMakerError { /* ... */ }

val espressoResult: Either<CoffeeMakerError, Espresso> = Right(Espresso(0.4)) val waterResult: Either<CoffeeMakerError, Water> = Left(RefillWater)

Either.applicative<CoffeeMakerError>() .map(espressoResult, waterResult) { (espresso, water) -> Americano(espresso, water) }.fix()

// Left(RefillWater)

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

More…

• Try // Success - Failure

• Validated // Valid - Invalid

• Eval // Now - Always - Later - Defer

• TupleN // Tuple2 - Tuple22

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

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

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

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.

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

Typeclasses

• Show

• Eq

• Semigroup

• Functor

• Monad

• …

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

Show

interface Show<in A> { fun A.show(): String }

The Show typeclass abstracts the ability to obtain a String representation of any object.

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

Show

interface Show<in A> { fun A.show(): String }

The Show typeclass abstracts the ability to obtain a String representation of any object.

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

Show

interface Show<in A> { fun A.show(): String }

The Show typeclass abstracts the ability to obtain a String representation of any object.

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

Show

interface Show<in A> { fun A.show(): String }

The Show typeclass abstracts the ability to obtain a String representation of any object.

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

Eq

interface Eq<in F> { 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.

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

Eq

interface Eq<in F> { 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.

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

Eq

interface Eq<in F> { 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.

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

Eq

interface Eq<in F> { 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.

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

Eq

interface Eq<in F> { 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.

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

Semigroup

interface Semigroup<A> { fun A.combine(b: A): A operator fun A.plus(b: A): A = this.combine(b) }

The Semigroup for some given type A has a single operation combine which takes two values of type A, and returns a value of type A. This operation must be associative.

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

Semigroup

interface Semigroup<A> { fun A.combine(b: A): A operator fun A.plus(b: A): A = this.combine(b) }

The Semigroup for some given type A has a single operation combine which takes two values of type A, and returns a value of type A. This operation must be associative.

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

Semigroup

interface Semigroup<A> { fun A.combine(b: A): A operator fun A.plus(b: A): A = this.combine(b) }

The Semigroup for some given type A has a single operation combine which takes two values of type A, and returns a value of type A. This operation must be associative.

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

Semigroup

interface Semigroup<A> { fun A.combine(b: A): A operator fun A.plus(b: A): A = this.combine(b) }

The Semigroup for some given type A has a single operation combine which takes two values of type A, and returns a value of type A. This operation must be associative.

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

Semigroup

interface Semigroup<A> { fun A.combine(b: A): A operator fun A.plus(b: A): A = this.combine(b) }

The Semigroup for some given type A has a single operation combine which takes two values of type A, and returns a value of type A. This operation must be associative.

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

Creating Instancesdata class Person(val firstName: String, val lastName: String) { companion object }

@extension interface PersonShow : Show<Person> { override fun Person.show(): String = "$firstName $lastName" }

val john = Person("John", "Doe") Person.show().run { john.show() } // John Doe

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

More…

• Hash

• Order

• Traverse

• Applicative

• And many more…

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

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

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

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 }

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

Without Lens

val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") )

val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

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

Without Lens

val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") )

val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

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

Without Lens

val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") )

val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

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

Without Lens

val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") )

val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

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

Without Lens

val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") )

val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

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

Without Lens

val profile = UserProfile( Name("John", "Doe"), Contact("[email protected]", "6483920164") )

val updatedProfile = profile.copy( contact = profile.contact.copy( email = "[email protected]" ) )

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile) // “[email protected]

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

With Lens

val profile = UserProfile( /* … */ )

val emailLens: Lens<UserProfile, String> = UserProfile.contact.email

val workEmail = "[email protected]" val updatedProfile = emailLens.modify(profile) { workEmail }

val email = emailLens.get(profile)

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

Optional

@optics data class ShoppingCart( val id: String, val cartItems: ListK<CartItem> ) { companion object }

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

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

Optional

@optics data class ShoppingCart( val id: String, val cartItems: ListK<CartItem> ) { companion object }

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

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

Optional

@optics data class ShoppingCart( val id: String, val cartItems: ListK<CartItem> ) { companion object }

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

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

Optional

@optics data class ShoppingCart( val id: String, val cartItems: ListK<CartItem> ) { companion object }

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

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

Optional

@optics data class ShoppingCart( val id: String, val cartItems: ListK<CartItem> ) { companion object }

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

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

Optional

@optics data class ShoppingCart( val id: String, val cartItems: ListK<CartItem> ) { companion object }

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

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

Optional

@optics data class ShoppingCart( val id: String, val cartItems: ListK<CartItem> ) { companion object }

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

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Set)

val shoppingCart = ShoppingCart("CRT19321", cartItems)

val quantityOptional: Optional<ShoppingCart, Int> = ShoppingCart .cartItems .index(ListK.index(), productIndex) .quantity

val updatedCart = quantityOptional.modify(shoppingCart) { it + 1 }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

Optional (Get)

val cartItemOption = ShoppingCart .cartItems .index(ListK.index(), productIndex) .getOption(shoppingCart)

val sameCartItemOption = ListK.index<CartItem>().run { ShoppingCart.cartItems[productIndex].getOption(shoppingCart) }

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

More…

• Iso

• Prism

• Getter / Setter

• Fold

• Each

• And others …

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

That’s not it…

• Arrow Fx

• Effects

• Arrow Query Language

• Generic

• Integrations

• Free

• Recursion Schemes

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

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

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

end;@ragunathjawaharTwitter / Medium / GitHub

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