February 2018
Intermediate to advanced
350 pages
7h 35m
English
Kotlin provides us with extension functions. What are they? They are like an ad hoc function on top of an existing datatype/class.
For example, if we want to count the number of words in a string, the following would be a traditional function to do it:
fun countWords(text:String):Int {
return text.trim()
.split(Pattern.compile("\s+"))
.size
}
We would pass a String to a function, have our logic count the words, and then we would return the value.
But don't you feel like it would always be better if there was a way that this function could be called on the String instance itself? Kotlin allows us to perform such an action.
Have a look at the following program:
fun String.countWords():Int { return trim() .split(Pattern.compile("\s+")) ...Read now
Unlock full access