11. × Java’s null-unsafety !!!!!!!!!!!!!!!!!!!!!!!!!!!
× Android doesn’t have the new bells and whistles of
Java (8+)
× Java’s base is procedural, not functional
Why NOT JAVA
12. × Kotlin is 100% interoperable with Java
× Yes, really.
× Kotlin isn’t as verbose
× Everybody knows… the less code, the less bugs
× Kotlin is fun (pun intended)
× And we have var and val, let, apply….
Why kotlin
13. × Extensions!
× Very fast learning curve
× You can do magic with it, no, for real!
Why kotlin
14. × Kotlin Standard Library adds 800Kb to the .apk
× Kotlin is from Russia. You need Vodka.
× Ok... it’s just the first one.
Drawbacks
40. Let’s get the magic going
OPERATORS COLLECTIONS MAGIC
41. val list = listOf(1, 2, 3, 4, 5, 6)
//Returns true if at least one element matches the given predicate.
list.any { it % 2 == 0 } //TRUE
list.any { it > 10 } // FALSE
//Returns true if all match the given predicate
list.all { it < 10 } //TRUE
list.all { it % 2 == 0 } //FALSE
//Returns the number of elements matching the given predicate.
list.count { it % 2 == 0 } // 3
42. val list = listOf(1, 2, 3, 4, 5, 6)
//Accumulates the value: starts with initial value
//and applies an operation from the first to the last
element in a collection.
list.fold(4) { total, next -> total + next } //25
//Same as fold, but goes from the last element to first
list.foldRight(4) { total, next -> total + next } //25
//Same as fold without initial value
list.reduce { total, next -> total + next } //21
//Same as foldRight without initial value
list.reduceRight { total, next -> total + next } //21
43. val list = listOf(1, 2, 3, 4, 5, 6)
//No comments
list.forEach { println(it) }
//Also, no comments
list.forEachIndexed { index, value -> println("position $index contains a $value") }
//Returns Max, the dog
list.max() // 6
//Returns Minion
list.min() // 1
44. val list = listOf(1, 2, 3, 4, 5, 6)
//Returns the first element yielding the largest value of the given function or null if there are no elements.
list.maxBy { -it } //1
//Same logic for min
list.minBy { -it } //6
//If none of the elements matches condition
list.none { it % 7 == 0 } //TRUE
//Returns the sum of all values produced by the transform function from the elements in the collection
list.sumBy { it % 2 } //3
45. val list = listOf(1, 2, 3, 4, 5, 6)
//Returns a list containing all elements except first n elements.
list.drop(4) //5, 6
//Returns a list containing all elements except first elements that satisfy the given predicate.
list.dropWhile { it < 3 } //3, 4, 5, 6
//Returns a list containing all elements except last elements that satisfy the given predicate.
list.dropLastWhile { it > 4 } //1, 2, 3, 4
//Returns a list containing all elements matching the given predicate.
list.filter { it % 2 == 0 } //2, 4, 6
//Returns a list containing all elements not matching the given predicate.
list.filterNot { it % 2 == 0 } //1, 3, 5
46. val list = listOf(1, 2, 3, 4, 5, 6)
//Returns a list containing elements at specified indices.
list.slice(listOf(1, 3, 4)) //2, 4, 5
//Returns a list containing first n elements
list.take(2) //1, 2
//Returns a list containing last n elements.
list.takeLast(2) //5, 6
//Returns a list containing first elements satisfying the given predicate.
list.takeWhile { it < 3 } //1, 2
47. val list = listOf(1, 2, 3, 4, 5, 6)
//Returns a list containing the results of applying
//the given transform function to each element and
//its index of the original collection.
list.mapIndexed { index, it -> index * it }
//Returns true if the element is found in the collection.
list.contains(2) // TRUE
//Returns an element at the given index or throws an //IndexOutOfBoundsException if the index is out
//of bounds of this collection.
list.elementAt(1) //2
48. val list = listOf(1, 2, 3, 4, 5, 6)
//Iterates over the elements creating a new collection for each one,
//and finally flattens all the collections into a unique list containing all the elements.
list.flatMap { listOf(it, it + 1) } //1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7
//Returns a map of the elements in original collection grouped by the result of given function
list.groupBy { if (it % 2 == 0) "even" else "odd" }//mapOf("odd" to listOf(1, 3, 5), "even" to listOf(2, 4, 6)), 2
//Returns a list containing the results of applying the given transform function to each element of the original
collection.
list.map { it * 2 } //2, 4, 6, 8, 10, 12
49. val list = listOf(1, 2, 3, 4, 5, 6)
//Returns an element at the given index or the result of calling the default function if the index is out of bounds
of this collection.
list.elementAtOrElse(10, { 2 * it }) //20
//Returns an element at the given index or null if the index is out of bounds of this collection.
list.elementAtOrNull(10) // null
//Returns an element at the given index or throws an IndexOutOfBoundsException if the index is out of bounds
of this collection.
list.elementAt(1) //2
//Returns an element at the given index or the result of calling the default function if the index is out of bounds
of this collection.
list.elementAtOrElse(10, { 2 * it }) //20
50. val list = listOf(1, 2, 3, 4, 5, 6)
//Returns an element at the given index or null if
the index is out of bounds of this collection.
list.elementAtOrNull(10) // null
//Returns the first element matching the given predicate
, or null if no element was found.
list.firstOrNull { it % 7 == 0 } //null
//Returns the first index of element, or -1 if the collection does not contain element.
list.indexOf(4) //3
//Returns index of the first element matching the given predicate, or -1 if the collection does not contain such
element.
list.indexOfFirst { it % 2 == 0 } //1
51. val list = listOf(1, 2, 3, 4, 5, 6)//Returns index of the last element matching the given predicate, or -1 if the collection does not contain
such element.
list.indexOfLast { it % 2 == 0 } //5
//Returns the last element matching the given predicate.
list.last { it % 2 == 0 } // 6
//Returns the single element matching the given predicate, or throws exception if there is no or more
than one matching element.
list.single { it % 5 == 0 } // 5
//Returns the single element matching the given predicate, or null if element was not found or more than
one element was found.
list.singleOrNull { it % 7 == 0 } // null
52. val list = listOf(1, 2, 3, 4, 5, 6)
//Splits original collection into pair of collections, where the first collection contains elements for which
the predicate returned true, while the second collection contains elements for which the predicate
returned false.
list.partition { it % 2 == 0 } //listOf(2, 4, 6), listOf(1, 3, 5)
//Returns a list containing all elements of the original collection and all elements of the given collection.
We can use the ‘+’ operator with it.
list + listOf(7, 8) //1, 2, 3, 4, 5, 6, 7, 8
//Returns a list of pairs built from the elements of both collections with the same indexes. The list has the
length of the shortest collection.
list.zip(listOf(7, 8)) //listOf(Pair(1, 7), Pair(2, 8))
53. Let’s get the magic going
OPERATORS COLLECTIONS MAGIC
54. ENOUGH SLIDES, code, Bs
LET’S CODE!
WE HAVE AN APP IN JAVA.
LET’S KOTLIN'IZE IT!
63. OTHER Things we missed
Higher-Order Functions and Lambdas
Inline functions
String templates
Properties
Class delegation
Java to Kotlin calls
Lambdas out of the box
val vs var
Apply
Sealed classes
Lazy & late init
64. Credits
Special thanks to all the people who made and
released these awesome resources for free:
× Presentation template by SlidesCarnival
× Photographs by Startupstockphotos