Skip to content

Instantly share code, notes, and snippets.

@burhanloey
Last active August 5, 2017 02:22
Show Gist options
  • Save burhanloey/ecb8af9c16c616485d0ca7c14b023240 to your computer and use it in GitHub Desktop.
Save burhanloey/ecb8af9c16c616485d0ca7c14b023240 to your computer and use it in GitHub Desktop.
Constructing DSL to make readable Malay language sentence using Kotlin's extension functions
package kotlinplayground
fun List<Int>.kemudian() = apply { }
fun List<Int>.dan() = apply { }
fun Int.sehingga(x: Int): IntRange = rangeTo(x)
fun diberiSenarai(x : IntRange): List<Int> = x.toList()
fun List<Int>.ambikNomborGanjil(): List<Int> = filter { it % 2 != 0 }
fun List<Int>.kaliDua(): List<Int> = map { it * 2 }
fun List<Int>.jumlahkan(): Int = sum()
fun main(args: Array<String>) {
val jawapan = diberiSenarai(1.sehingga(9)).ambikNomborGanjil().kemudian().kaliDua().dan().jumlahkan()
print(jawapan)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment