Why do we need advancing?
• Stuck on Java 6(.5)ish
Slide 4
Slide 4 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time
Slide 5
Slide 5 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time
• No streams
Slide 6
Slide 6 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time
• No streams
• No lambdas, method refs, non-capturing anon class
Slide 7
Slide 7 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time
• No streams
• No lambdas, method refs, non-capturing anon class
• No try-with-resources
Slide 8
Slide 8 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time Use ThreeTenBP / ThreeTenABP
• No streams
• No lambdas, method refs, non-capturing anon class
• No try-with-resources
Slide 9
Slide 9 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time Use ThreeTenBP / ThreeTenABP
• No streams Use backport or RxJava
• No lambdas, method refs, non-capturing anon class
• No try-with-resources
Slide 10
Slide 10 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time Use ThreeTenBP / ThreeTenABP
• No streams Use backport or RxJava
• No lambdas, method refs, non-capturing anon class Use Retrolambda
• No try-with-resources
Slide 11
Slide 11 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• No javax.time Use ThreeTenBP / ThreeTenABP
• No streams Use backport or RxJava
• No lambdas, method refs, non-capturing anon class Use Retrolambda
• No try-with-resources minSdkVersion=19 or Retrolambda
Slide 12
Slide 12 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
Slide 13
Slide 13 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• Java language restrictions and problems
Slide 14
Slide 14 text
Why do we need advancing?
• Java language restrictions and problems
Slide 15
Slide 15 text
Why do we need advancing?
• Java language restrictions and problems
• Inability to add methods to platform types
Slide 16
Slide 16 text
Why do we need advancing?
• Java language restrictions and problems
• Inability to add methods to platform types ("Util" hell)
Slide 17
Slide 17 text
Why do we need advancing?
• Java language restrictions and problems
• Inability to add methods to platform types ("Util" hell)
• Nullability problems
Slide 18
Slide 18 text
Why do we need advancing?
• Java language restrictions and problems
• Inability to add methods to platform types ("Util" hell)
• Nullability problems
• General verbosity of common idioms
Slide 19
Slide 19 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• Java language restrictions and problems
Slide 20
Slide 20 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• Java language restrictions and problems
• Android API design problems
Slide 21
Slide 21 text
Why do we need advancing?
• Android API design problems
Slide 22
Slide 22 text
Why do we need advancing?
• Android API design problems
• Inheritance party
Slide 23
Slide 23 text
Why do we need advancing?
• Android API design problems
• Inheritance party
• Nullability everywhere
Slide 24
Slide 24 text
Why do we need advancing?
• Android API design problems
• Inheritance party
• Nullability everywhere
• Ceremony of APIs
Slide 25
Slide 25 text
Why do we need advancing?
• Stuck on Java 6(.5)ish
• Java language restrictions and problems
• Android API design problems
Slide 26
Slide 26 text
Syntax Crash Course
Slide 27
Slide 27 text
Syntax Crash Course
fun sum(a: Int, b: Int): Int {
return a + b
}
Slide 28
Slide 28 text
Syntax Crash Course
fun sum(a: Int, b: Int): Int {
return a + b
}X
Slide 29
Slide 29 text
Syntax Crash Course
fun sum(a: Int, b: Int): Int {
return a + b
}X
Slide 30
Slide 30 text
Syntax Crash Course
fun sum(a: Int, b: Int): Int {
return a + b
}X
Slide 31
Slide 31 text
Syntax Crash Course
fun sum(a: Int, b: Int) = a + b
Slide 32
Slide 32 text
Syntax Crash Course
fun main(args: Array) {
println("Args: $args")
}
Slide 33
Slide 33 text
Syntax Crash Course
fun main(args: Array) {
println("Args: $args")
}X
Slide 34
Slide 34 text
Syntax Crash Course
fun main(args: Array) {
println("Args: $args")
}X
Slide 35
Slide 35 text
Syntax Crash Course
fun main(args: Array) {
println("Args: $args")
}X
Slide 36
Slide 36 text
Syntax Crash Course
val name = "Jake"
val people: List = ArrayList()
Slide 37
Slide 37 text
Syntax Crash Course
val name = "Jake"
val people: List = ArrayList()
Slide 38
Slide 38 text
Syntax Crash Course
val name = "Jake"
val people: List = ArrayList()
Slide 39
Slide 39 text
Syntax Crash Course
val name = "Jake"
val people: List = ArrayList()
Slide 40
Slide 40 text
Syntax Crash Course
val name = "Jake"
val people: List = ArrayList()
Extension Functions
String firstName1= null;
int firstNameColumn = cursor.getColumnIndexOrThrow("first_name");
if (!cursor.isNull(firstNameColumn)) {
firstName3=2cursor.getString(firstNameColumn);
}
Slide 70
Slide 70 text
Extension Functions
String firstName1= null;
int firstNameColumn = cursor.getColumnIndexOrThrow("first_name");
if (!cursor.isNull(firstNameColumn)) {
firstName3=2cursor.getString(firstNameColumn);
}
fun Cursor.getStringOrNull(columnName: String): String? {
val index = getColumnIndexOrThrow(columnName)
return if (isNull(index)) null else getString(index)
}X
fun Cursor.getString(columnName: String): String =
getStringOrNull(columnName)!!
Slide 71
Slide 71 text
Extension Functions
fun Cursor.getStringOrNull(columnName: String): String? {
val index = getColumnIndexOrThrow(columnName)
return if (isNull(index)) null else getString(index)
}X
fun Cursor.getString(columnName: String): String =
getStringOrNull(columnName)!!
Slide 72
Slide 72 text
Extension Functions
fun Cursor.getStringOrNull(columnName: String): String? {
val index = getColumnIndexOrThrow(columnName)
return if (isNull(index)) null else getString(index)
}X
fun Cursor.getString(columnName: String): String =
getStringOrNull(columnName)!!
val firstName = cursor.getStringOrNull("first_name")
Slide 73
Slide 73 text
Extension Functions
fun Cursor.getStringOrNull(columnName: String): String? {
val index = getColumnIndexOrThrow(columnName)
return if (isNull(index)) null else getString(index)
}X
fun Cursor.getString(columnName: String): String =
getStringOrNull(columnName)!!
val firstName = cursor.getStringOrNull("first_name")
Slide 74
Slide 74 text
Extension Functions
fun Cursor.getStringOrNull(columnName: String): String? {
val index = getColumnIndexOrThrow(columnName)
return if (isNull(index)) null else getString(index)
}X
fun Cursor.getString(columnName: String): String =
getStringOrNull(columnName)!!
val firstName = cursor.getStringOrNull("first_name")
Slide 75
Slide 75 text
Extension Functions
fun Cursor.getStringOrNull(columnName: String): String? {
val index = getColumnIndexOrThrow(columnName)
return if (isNull(index)) null else getString(index)
}X
fun Cursor.getString(columnName: String): String =
getStringOrNull(columnName)!!
val firstName = cursor.getStringOrNull("first_name")
Slide 76
Slide 76 text
Function Expressions
Slide 77
Slide 77 text
Function Expressions
{ it.toString() }
Slide 78
Slide 78 text
Function Expressions
{ x, y -> x + y }
{ it.toString() }
Slide 79
Slide 79 text
Function Expressions
{ x, y -> x + y }
{ x: Int, y: Int -> x + y }
{ it.toString() }
{ x, y -> x + y }
{ x: Int, y: Int -> x + y }
Slide 80
Slide 80 text
Function Expressions
{ x, y -> x + y }
{ x: Int, y: Int -> x + y }
val sum = { x: Int, y: Int -> x + y }
val sum: (Int, Int) -> Int = { x, y -> x + y }
{ it.toString() }
Function Expressions
val notEmpty: (String) -> Boolean { !it.isEmpty() }
val atLeastFour: (String) -> Boolean { it.length() > 4 }
val fourDigits: (String) -> Boolean { it.matches("\d{4}") }
val validCreditCard: (String) -> Boolean { luhnCheck(it) }
Slide 83
Slide 83 text
Higher-Order Functions
Slide 84
Slide 84 text
Higher-Order Functions
fun apply(one: Int, two: Int, func: (Int, Int) -> Int): Int {
return func(one, two)
}
Slide 85
Slide 85 text
Higher-Order Functions
fun apply(one: Int, two: Int, func: (Int, Int) -> Int): Int {
return func(one, two)
}
val sum = apply(1, 2, { x, y -> x + y })
Slide 86
Slide 86 text
Higher-Order Functions
fun apply(one: Int, two: Int, func: (Int, Int) -> Int): Int {
return func(one, two)
}X
val sum = apply(1, 2, { x, y -> x + y }
val difference = apply(1, 2, { x, y -> x - y }
)
)
Slide 87
Slide 87 text
Higher-Order Functions
fun apply(one: Int, two: Int, func: (Int, Int) -> Int): Int {
return func(one, two)
}X
val sum = apply(1, 2) { x, y -> x + y }
val difference = apply(1, 2) { x, y -> x - y }
Slide 88
Slide 88 text
Higher-Order Functions
fun apply(one: Int, two: Int, func: (Int, Int) -> Int): Int {
return func(one, two)
}X
Slide 89
Slide 89 text
Higher-Order Functions
fun Int.apply(two: Int, func: (Int, Int) -> Int): Int {
return func(this, two)
}X
Slide 90
Slide 90 text
Higher-Order Functions
fun Int.apply(other: Int, func: (Int, Int) -> Int): Int {
return func(this, other)
}X
Slide 91
Slide 91 text
Higher-Order Functions
fun Int.apply(other: Int, func: (Int, Int) -> Int): Int {
return func(this, other)
}
val sum = 1.apply(2) { x, y -> x + y }
val difference = 1.apply(2) { x, y -> x - y }
Slide 92
Slide 92 text
Higher-Order Functions
fun Int.apply(other: Int, func: (Int, Int) -> Int): Int {
return func(this, other
}X
)
Slide 93
Slide 93 text
Higher-Order Functions
fun Int.apply(func: (Int) -> Int): Int {
return func(this)
}X
Slide 94
Slide 94 text
Higher-Order Functions
fun Int.apply(func: (Int) -> Int): Int {
return func(this)
}X
val double = 1.apply { x -> x * 2 }
Slide 95
Slide 95 text
Higher-Order Functions
fun Int.apply(func: (Int) -> Int): Int {
return func(this)
}X
val double = 1.apply { it * 2 }
Slide 96
Slide 96 text
Higher-Order Functions
fun List.filter(predicate: (T) -> Boolean): List {
}X
Slide 97
Slide 97 text
Higher-Order Functions
fun List.filter(predicate: (T) -> Boolean): List {
val newList = ArrayList()
return newList
}X
Slide 98
Slide 98 text
Higher-Order Functions
fun List.filter(predicate: (T) -> Boolean): List {
val newList = ArrayList()
for (item in this) {
}Y
return newList
}X
Slide 99
Slide 99 text
Higher-Order Functions
fun List.filter(predicate: (T) -> Boolean): List {
val newList = ArrayList()
for (item in this) {
if (predicate(item)) {
}Z
}Y
return newList
}X
Slide 100
Slide 100 text
Higher-Order Functions
fun List.filter(predicate: (T) -> Boolean): List {
val newList = ArrayList()
for (item in this) {
if (predicate(item)) {
newList.add(item)
}Z
}Y
return newList
}X
Slide 101
Slide 101 text
Higher-Order Functions
fun List.filter(predicate: (T) -> Boolean): List {
val newList = ArrayList()
for (item in this) {
if (predicate(item)) {
newList.add(item)
}
}
return newList
}
val names = listOf("Jake", "Jesse", "Matt", "Alec")
val jakes = names.filter { it == "Jake" }
Higher-Order Functions
data class Lock(private val obj: T) {
public fun acquire(func: (T) -> Unit) {
synchronized (obj) {
func(obj)
}
}
}
Slide 105
Slide 105 text
Higher-Order Functions
data class Lock(private val obj: T) {
public fun acquire(func: (T) -> Unit) {
synchronized (obj) {
func(obj)
}
}
}
val readerLock = Lock(JsonReader(stream))
Slide 106
Slide 106 text
Higher-Order Functions
data class Lock(private val obj: T) {
public fun acquire(func: (T) -> Unit) {
synchronized (obj) {
func(obj)
}
}
}
val readerLock = Lock(JsonReader(stream))
// Later
readerLock.acquire {
println(it.readString())
}
Slide 107
Slide 107 text
Higher-Order Functions
val notEmpty: (String) -> Boolean { !it.isEmpty() }
val atLeastFour: (String) -> Boolean { it.length() > 4 }
val fourDigits: (String) -> Boolean { it.matches("\\d{4}") }
val validCreditCard: (String) -> Boolean { luhnCheck(it) }
Extension Function Expressions
• Extension Functions — Functions added to a type without modifying
the original.
Slide 113
Slide 113 text
Extension Function Expressions
• Extension Functions — Functions added to a type without modifying
the original.
• Function Expressions — Undeclared function bodies used an as
expression (i.e., as data).
Slide 114
Slide 114 text
Extension Function Expressions
• Extension Functions — Functions added to a type without modifying
the original.
• Function Expressions — Undeclared function bodies used an as
expression (i.e., as data).
• Higher-Order Functions: A function which takes a function or returns
a function.