raulraja
07/19/2017, 11:06 PMtailrec
and immutable maps. May be buggy but thought it illustrated his point and may give you more to think about.
tailrec fun loop(list: List<String>, acc: Pair<String, Any>): Map<String, Any> =
when {
list.isEmpty() -> mapOf(acc)
else -> {
val tail = list.drop(1)
loop(tail,
if (tail.isEmpty()) acc
else Pair(tail[0], mapOf(acc)))
}
}
fun main(args: Array<String>): Unit {
val s = "a.b.c.d = 20" //hacky cleanup, should be done with proper parsers
val exp = s.replace(" ", "").split("=")
val lhs = exp[0].split(".").reversed()
val rhs = exp[1]
val result = loop(lhs, Pair(lhs[0], rhs))
println(result) // {a={b={c={d=20}}}}
}