Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin in Production

Kotlin in Production

goncalossilva

May 25, 2017
Tweet

More Decks by goncalossilva

Other Decks in Programming

Transcript

  1. data class Customer(val name: String, val purchases: List<Purchase>) data class

    Purchase(val value: Int, val success: Boolean) object Dataset { val customers: MutableList<Customer> = ArrayList() init { customers.add(Customer( "Gonçalo”, arrayListOf(Purchase(42, true), Purchase(9000, false)))) } }
  2. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  3. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  4. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  5. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  6. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  7. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  8. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() // Find the customer. val customer = Dataset.customers.find { it.name == name } // Gather successful purchases. if (customer != null) { successfulPurchases.addAll(customer.purchases.filter { it.success }) } return successfulPurchases }
  9. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  10. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  11. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  12. fun successfulPurchases(name: String) : List<Purchase> { return Dataset.customers.find { it.name

    == name }?.purchases? .filter { it.success } ?: emptyList() }
  13. fun successfulPurchases(name: String) : List<Purchase> { val successfulPurchases: MutableList<Purchase> =

    ArrayList() var customer: Customer? = null for (c in Dataset.customers) { if (c.name == name) { customer = c break } } if (customer != null) { for (purchase in customer.purchases) { if (purchase.success) successfulPurchases.add(purchase) } } return successfulPurchases }
  14. Higher-order functions, properties, mixins and delegation, extension functions, static nullability

    checking, automatic casts, reified generics, declaration-site variance, modules and build infrastructure, inline functions, operator overloading, String interpolation, pattern matching, first class IDE support, Java converter, default parameters, infix methods, destructuring declarations, and more...
  15. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  16. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  17. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  18. inline fun SQLiteDatabase.inTransaction(func: SQLiteDatabase.() -> Unit) { beginTransaction() try {

    func() setTransactionSuccessful() } finally { endTransaction() } } db.inTransaction { delete("hackathon", "name = ?", arrayOf("Pixels Camp")) }
  19. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  20. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  21. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  22. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }
  23. fun Cursor.getStringOrNull(columnName: String): String? { val index = getColumnIndexOrThrow(columnName) return

    if (isNull(index)) null else getString(index) } fun View.setPaddingTop(top: Int) { setPadding(paddingLeft, top, paddingRight, paddingBottom) } fun View.toBitmap() : Bitmap? { if (width > 0 && height > 0) { val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) val canvas = Canvas(bitmap) draw(canvas) return bitmap } else { return null } }