Skip to content

Instantly share code, notes, and snippets.

@wertlex
Created June 18, 2015 18:43
Show Gist options
  • Save wertlex/c97d7fa852b4e5827633 to your computer and use it in GitHub Desktop.
Save wertlex/c97d7fa852b4e5827633 to your computer and use it in GitHub Desktop.
def countTokenized: Iteratee[String, List[Int]] = {
def step(restCount: Int, resultList: List[Int])(i: Input[String]): Iteratee[String, List[Int]] = i match {
case Input.Empty =>
Done(Nil, Input.Empty)
case Input.EOF => {
(restCount, resultList) match {
case (0, Nil) => Done(Nil, Input.EOF)
case (0, resList) => Done(resList, Input.EOF)
case (rCount, Nil) => Done(List(rCount), Input.EOF)
case (rCount, resList) => Done(resultList ::: List(restCount), Input.EOF)
}
}
case Input.El(e) =>
if(e.contains(",")) { // there are "," in that chunk
val lengthList = e.split(",", -1).toList.map(_.length)
val newResultList = lengthList.dropRight(1).updated(0, restCount + lengthList.head)
val newRestCount = lengthList.last
Cont[String, List[Int]](i => step(newRestCount, resultList ::: newResultList)(i))
} else { // chunk without ","
Cont[String, List[Int]](i => step(e.length + restCount, resultList)(i)) // pass e.length to next step
}
}
Cont[String, List[Int]](i => step(0, Nil)(i))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment