Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.94 KB | None | 0 0
  1. fun parse(path: String): Map<String, Map<String, Recipe>> {
  2.         val config = JSON.parse<Json>(getFile(path))
  3.  
  4.         return config.iterator().asSequence().map { child ->
  5.             Recipe(
  6.                     name = child["name"].toString(),
  7.                     category = child["category"].toString(),
  8.                     energy = child["energy"].toString().toFloat(),
  9.                     inputs = child["ingredients"].unsafeCast<Json>().iterator().asSequence().map {
  10.                         it["name"].toString() to it["amount"].toString().toInt()
  11.                     }.toList(),
  12.                     outputs = child["products"].unsafeCast<Json>().iterator().asSequence().map {
  13.                         it["name"].toString() to it["amount"].toString().toInt()
  14.                     }.toList()
  15.             )
  16.         }.groupBy({ it.category }, { (it.name to it) })
  17.                 .map { it.key to it.value.toMap() }
  18.                 .toMap()
  19.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement