Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Building DSLs

Jossi Wolf
January 28, 2019

Building DSLs

Lightning talk from KUGHH Meetup February 2019

Jossi Wolf

January 28, 2019
Tweet

More Decks by Jossi Wolf

Other Decks in Programming

Transcript

  1. animation { 0.frame { x = 10 y = 20

    } 50.frame { y = 70 rotation = 90 } 100.frame { x = 0 y = -20 rotation = 40 } }
  2. /** * Example of a higher-order function */ fun<T, R>

    Iterable<T>.map(transform: (T) -> R): MutableList<R> { val destination = emptyMutableList<R>() this.forEach { value -> // Invoke the transform function with the current value val transformedValue = transform(value) destination.add(transformedValue) } return destination }
  3. /** * Example of a higher-order function */ fun<T, R>

    Iterable<T>.map(transform: (T) -> R): MutableList<R> { val destination = emptyMutableList<R>() this.forEach { value -> // Invoke the transform function with the current value val transformedValue = transform(value) destination.add(transformedValue) } return destination }
  4. /** * Calls the specified function [block] with `this` value

    * as its receiver and returns `this` value. */ public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this }
  5. /** * Calls the specified function [block] with `this` value

    * as its receiver and returns `this` value. */ public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this }
  6. /** * Calls the specified function [block] with `this` value

    * as its receiver and returns `this` value. */ public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this }
  7. /** * Calls the specified function [block] with `this` value

    * as its receiver and returns `this` value. */ public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block() return this }
  8. /** * Calls the specified function [block] with `this` value

    * as its receiver and returns `this` value. */ public inline fun <T> T.apply(block: T.() -> Unit): T { contract { callsInPlace(block, InvocationKind.EXACTLY_ONCE) } block(this) return this }
  9. val user = User(name = "John", age = 30, city

    = "Berlin") user.apply { age = 31 city = "Hamburg" }
  10. val user = User(name = "John", age = 30, city

    = "Berlin") user.apply { this.age = 31 this.city = "Hamburg" }
  11. animation { // This is a higher-order function 0.frame {

    x = 10 y = 20 } 50.frame { y = 70 rotation = 90 } 100.frame { x = 0 y = -20 rotation = 40 } }
  12. animation { frame(0) { x = 10 y = 20

    } 0.frame { x = 10 y = 20 } }
  13. animation { frame(0) { x = 10 y = 20

    } 0.frame { x = 10 y = 20 } 20 { rotation = 40 } }