Advanced programming with Kotlin
Kotlin has a lot of features that can be used for advanced programming. This section will explain some of those.
Functions
Unlike in Java, where classes are first-class citizens, functions are first class citizens in Kotlin. This means that a function can exist without being inside a class. This is quite powerful and there are a lot of advanced ways functions can be used to achieve different functionalities.
Infix notation in functions
Infix notations can be used to define functions that can be used, such as 1 + 2, 2 - 3, true && false, a == b
statements where there is an operator and two operands. The following function with the infix
notation can be used to ZIP (interweave) words together in a way that the letters of each String
are joined together to create a new String
that has overlapping letters:
infix fun String.zip(s1 : String) : String { var result : String = ""; var zipLength : Int = Math.min(s1.length, this.length); for(i in 0..zipLength...