commonMain.Hierarchy.kt Maven / Gradle / Ivy
package com.juul.krayon.hierarchy
public fun hierarchy(
root: T,
getChildren: (T) -> List,
): Node {
fun addChildren(parentNode: Node) {
val children = getChildren(parentNode.data)
parentNode.children = children.map { child ->
Node(child, null, parentNode).also(::addChildren)
}
}
return Node(root, null).also(::addChildren)
}
public fun flatHierarchy(
values: Iterable,
): Node = Node(null as T?, null).also { root ->
root.children = values.map { value -> Node(value, null, root) }
}