class Chainer(…) { infix fun joinedWith(option: Joiner) = initials.reduce { previous, letter !" val next = when (option) { is noPunctuaction !" letter is dots !" "$letter." } "$previous$next" }
class Chainer(…) { infix fun joinedWith(option: Joiner) = initials.reduce { previous, letter !" val next = when (option) { is noPunctuaction !" letter is dots !" "$letter." } "$previous$next" }
class Chainer(…) { infix fun joinedWith(option: Joiner) = initials.reduce { previous, letter !" val next = when (option) { is noPunctuaction !" letter is dots !" "$letter." } "$previous$next" }
class Chainer(…) { infix fun joinedWith(option: Joiner) = initials.reduce { previous, letter !" val next = when (option) { is noPunctuaction !" letter is dots !" "$letter." } "$previous$next" }
val items = mutableListOf(1, 2, 3) items += 4 /#$ * Adds the specified [element] to this mutable collection. %& @kotlin.internal.InlineOnly public inline operator fun MutableCollection.plusAssign(element: T) { this.add(element) }
operator fun Date.plus(args: IntPair): Date { calendar.time = this calendar.add(args.first, args.second) return calendar.time } operator fun Date.minus(args: IntPair): Date { calendar.time = this calendar.add(args.first, -args.second) return calendar.time }
operator fun Date.plus(args: IntPair): Date { calendar.time = this calendar.add(args.first, args.second) return calendar.time } operator fun Date.minus(args: IntPair): Date { calendar.time = this calendar.add(args.first, -args.second) return calendar.time }
fun Int.days() = Calendar.DAY_OF_YEAR to this fun Int.months() = Calendar.MONTH to this val atPast = Date() - 2.months() val atFuture = Date() + 15.days()
// Add more hooks to same span() function fun italic(given: CharSequence) = span(given, StyleSpan(Typeface.ITALIC)) fun underline(given: CharSequence) = span(given, UnderlineSpan()) fun bold(given: CharSequence) = span(given, StyleSpan(Typeface.BOLD))
val add = fun(a: Int, b: Int) = a + b fun calculate(func: (Int, Int) !" Int) { func.invoke(2,2) } // Trailling Notation calculate { a, b !" a + b } // Normal notation calculate(add)
class Receiver(val data: Int) // Kotlin allows us to add an extension function // literal to a type. Such lambda acquires the // properties of non-static method in the context val addOne: Receiver.() !" Int = { data + 1 } val two = addOne(Receiver(1)) val twoAgain = Receiver(1).addOne()
class FilmFields { val title: String get() { … } val director: String get() { … } private companion object { const val TITLE = "title" const val DIRECTOR = "director" } }
// Plain old mockWebServer val server = MockWebServer() val response = MockResponse().apply { setResponseCode(503) setBody("Ops!") } server.enqueue(response) server.start()
class MockResponseBuilder( '( Aliases var code: Int = 0, var response: String? = null) { fun mockResponse() = MockResponse().apply { setResponseCode(code) setBody(response) } }
CONCLUSIONS • DSLs are fun (with no pun) • DSL-building offer great insights over Kotlin features! • DSLs should work to improve an existing domain, not replace it • Design your own DSLs for fun and profit !