File tree Expand file tree Collapse file tree 3 files changed +64
-0
lines changed
src/main/kotlin/com/github/h0tk3y/kotlinMonads Expand file tree Collapse file tree 3 files changed +64
-0
lines changed Original file line number Diff line number Diff line change @@ -27,4 +27,8 @@ dependencies {
2727task sourcesJar (type : Jar , dependsOn : classes) {
2828 classifier = ' sources'
2929 from sourceSets. main. allSource
30+ }
31+
32+ artifacts {
33+ archives sourcesJar
3034}
Original file line number Diff line number Diff line change 1+ package com.github.h0tk3y.kotlinMonads
2+
3+ fun json (block : JsonContext .() -> Unit ) = JsonContext ().run {
4+ block()
5+ " {\n " + output.toString() + " \n }"
6+ }
7+
8+ class JsonContext internal constructor() {
9+ internal val output = StringBuilder ()
10+
11+ private var indentation = 4
12+
13+ private fun StringBuilder.indent () = apply {
14+ for (i in 1 .. indentation)
15+ append(' ' )
16+ }
17+
18+ private var needsSeparator = false
19+
20+ private fun StringBuilder.separator () = apply {
21+ if (needsSeparator) append(" ,\n " )
22+ }
23+
24+ infix fun String.to (value : Any ) {
25+ output.separator().indent().append(" \" $this \" : \" $value \" " )
26+ needsSeparator = true
27+ }
28+
29+ infix fun String.toJson (block : JsonContext .() -> Unit ) {
30+ output.separator().indent().append(" \" $this \" : {\n " )
31+ indentation + = 4
32+ needsSeparator = false
33+ block(this @JsonContext)
34+ needsSeparator = true
35+ indentation - = 4
36+ output.append(" \n " ).indent().append(" }" )
37+ }
38+ }
39+
40+ fun main (args : Array <String >) {
41+ val j = json {
42+ " a" to 1
43+ " b" to " abc"
44+ " c" toJson {
45+ " d" to 123
46+ " e" toJson {
47+ " f" to " g"
48+ }
49+ }
50+ }
51+
52+ println (j)
53+ }
Original file line number Diff line number Diff line change 1+ package com.github.h0tk3y.kotlinMonads
2+
3+ val foo by lazy { " abc" }
4+
5+ var <T > List <T >.bar get() = foo; set(value) {
6+ println (foo)
7+ }
You can’t perform that action at this time.
0 commit comments