Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
221 lines (84 sloc) 1.67 KB
import java.util.ArrayList
fun main(args: Array<String>) {
// Higher-order functions
fun <T> let(t: T, body: (T) -> Unit) {
body(t)
}
let(2 + 3, { x -> println(x * x) })
let (2 + 3) {
x ->
println(x * x)
}
// Extensions functions
fun String.lastChar(): Char {
return this.charAt(this.size - 1)
}
println(
"abc".lastChar()
)
}
fun withDemo() {
// Extension functions as values
fun <T> let(t: T, body: (T) -> Unit) {
body(t)
}
fun <T> with(t: T, body: T.() -> Unit) {
t.body()
}
val stringBuilder = StringBuilder()
with(stringBuilder, { append("1") })
with (stringBuilder) {
append("Numbers: ")
for (i in 1..10) {
append(i)
}
}
}
// Basic mechanics of builders
fun demo1() {
class LI
class UL {
fun li() {}
}
with(UL()) {
li()
li()
li()
}
fun ul(init: UL.() -> Unit) {
val ul = UL()
ul.init()
}
ul {
li()
li()
li()
}
}
// How builders actually work
fun demo2() {
class LI
class UL {
val items = ArrayList<LI>()
fun li(init: LI.() -> Unit) {
val li = LI()
li.init()
items.add(li)
}
}
fun ul(init: UL.() -> Unit) {
val ul = UL()
ul.init()
}
ul {
li {
// ...
}
li {
// ...
}
}
}
fun <T> with(t: T, body: T.() -> Unit) {
t.body()
}