expression parsing, sorted list, tree, other shit; bump to 0.10

This commit is contained in:
alexander.nutz
2024-03-29 16:27:00 +01:00
parent d8c4c5a90d
commit b8bd109182
21 changed files with 748 additions and 50 deletions

View File

@@ -7,4 +7,47 @@ fun <T, R> Iterator<T>.mapModifier(fn: (T) -> R): Iterator<R> =
override fun hasNext(): Boolean =
this@mapModifier.hasNext()
}
}
fun <T> generateIterator(fn: () -> T?): Iterator<T> =
object: Iterator<T> {
var calc = false
var nx: T? = null
override fun hasNext(): Boolean {
if (!calc) {
nx = fn()
calc = true
}
return nx != null
}
override fun next(): T {
if (!hasNext())
throw Exception("iterator end")
calc = false
return nx!!
}
}
/**
* Can't explain this function. Look at the source of [blitz.parse.tokenize] as an example
*/
fun <T, R> Iterator<T>.funnyMap(fn: (UnGettableIterator<T>) -> R?): Iterator<R> {
val iter = asUnGettable()
return generateIterator { fn(iter) }
}
fun <T> Iterator<T>.collect(to: MutableList<T> = mutableListOf()): MutableList<T> {
forEachRemaining {
to.add(it)
}
return to
}
fun <T> Iterator<T>.collect(to: Vec<T>): Vec<T> {
forEachRemaining {
to.pushBack(it)
}
return to
}