
com.github.andyglow.tree.Result.scala Maven / Gradle / Ivy
package com.github.andyglow.tree
import scala.annotation.tailrec
/** The intermediate return type of the pretty-print system: provides an iterator which produces the actual string output, as well as metadata around that output that is only available after the
* iterator is exhausted
*/
class Result(val iter: Iterator[String], completedLineCount0: => Int, lastLineLength0: => Int) {
lazy val completedLineCount = {
require(iter.isEmpty)
completedLineCount0
}
lazy val lastLineLength = {
require(iter.isEmpty)
lastLineLength0
}
def flatMap(f: (Int, Int) => Result): Result = {
var newCompletedLineCount = 0
var newLastLineLength = 0
val mergedIterator = Util.concat(
() => iter,
() => {
require(!iter.hasNext)
val newResult = f(completedLineCount, lastLineLength0)
newResult.iter.map { x =>
if (!newResult.iter.hasNext) {
newCompletedLineCount = newResult.completedLineCount
newLastLineLength = newResult.lastLineLength
}
x
}
}
)
new Result(
mergedIterator,
newCompletedLineCount + completedLineCount,
if (newCompletedLineCount > 0) newLastLineLength
else newLastLineLength + lastLineLength
)
}
}
object Result {
def fromString(s: => String) = {
lazy val lines = s.linesIterator.toArray
new Result(Iterator(s), lines.length - 1, lines.last.length)
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy