8. Example : Suppose I want to measure the execution time of a function
val startTime = System.currentTimeMillis()
printSpeakerList()
val endTime = System.currentTimeMillis()
println("Executed in ${endTime - startTime}ms")
*https://hackernoon.com/a-practical-use-for-higher-order-functions-in-kotlin-7ff89cebf6fd
9. What if I want to measure the execution time of other lines of code ?
val startTime = System.currentTimeMillis()
printSpeakerList()
val endTime = System.currentTimeMillis()
println("Executed in ${endTime - startTime}ms")
val startTime = System.currentTimeMillis()
printAudienceList()
val endTime = System.currentTimeMillis()
println("Executed in ${endTime - startTime}ms")
val startTime = System.currentTimeMillis()
printOrganizerList()
val endTime = System.currentTimeMillis()
println("Executed in ${endTime - startTime}ms")
10. Make it DRY: Define a higher order function that accept function
val startTime = System.currentTimeMillis()
val endTime = System.currentTimeMillis()
println("Executed in ${endTime - startTime}ms")
fun printExecutionTime( block: () -> Unit ) {
}
block()
//Operation your want to measure
11. printExecutionTime ({
printOrganizerList()
})
Using our higher order function
printExecutionTime ({
printAudienceList()
})
printExecutionTime ({
printSpeakerList()
})
fun printExecutionTime(block: () -> Unit) {
val startTime = System.currentTimeMillis()
block()
val endTime = System.currentTimeMillis()
println("Executed in ${endTime - startTime}ms")
}
Higher Order Function
Lambda
13. Setting Status bar color : Only available after Lollipop
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(Color.BLACK)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(Color.BLACK)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(Color.BLACK) }
* https://www.androidauthority.com/android-m-dark-status-bar-icons-613241/
14. blocks of code that are only executed if the version is Lollipop or newer:
supportsLollipop ({
window.setStatusBarColor(Color.BLACK)
})
inline fun supportsLollipop(block: () -> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
block()
}
}
supportsLollipop {
window.setStatusBarColor(Color.BLACK)
}
15. Also work when checking Build Variant
inline fun debug(block:() -> Unit) {
if (BuildConfig.DEBUG) {
block()
}
}
debug {
// do something
}
16. Extension Function
“Ability to extend a class with new functionality without
having to inherit from the class or use any type of design
pattern.”
17. Extension function example: String to uri
val uri1 = Uri.parse(“https://stackoverflow.com”)
val uri2 = Uri.parse(“https://youtube.com”)
/**
* Creates a Uri from the given encoded URI string.
*
* @see Uri.parse
*/
fun String.toUri(): Uri = Uri.parse(this)
val uri1 = “https://stackoverflow.com”.toUri()
val uri2 = “https://youtube.com”.toUri()
18. Extension function example: Create toast in Fragment
Toast.makeText(context, "Hello MobileConf 2018", Toast.LENGTH_LONG).show()
Toast.makeText(context, "Hello MobileConf 2018", Toast.LENGTH_SHORT).show()
fun Fragment.longToast(text: String) {
Toast.makeText(context, text, Toast.LENGTH_LONG).show()
}
fun Fragment.shortToast(text: String) {
Toast.makeText(context, text, Toast.LENGTH_SHORT).show()
}
longToast("Hello MobileConf 2018")
shortToast( "Hello MobileConf 2018")
24. Where should we declare extension function ?
Extension function should be declare as top-level function ( function that
locate outside of the class )
25. Inline function ?
An inline function will be substituted by its code during compilation, instead of
doing the real call to a function. It will reduce memory allocations and runtime
overhead in some situations.
inline fun debug(block:() -> Unit) {
if (BuildConfig.DEBUG) {
block()
}
}
inline fun supportsLollipop(block: () -> Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
block()
}
}
26. Conclusion
- Higher order function is function that accept or return function
- Extension function can add more functionality to existing class
- They both help make our code DRYer