Skip to content

Instantly share code, notes, and snippets.

@tksk
Last active December 21, 2015 00:49
Show Gist options
  • Save tksk/6223397 to your computer and use it in GitHub Desktop.
Save tksk/6223397 to your computer and use it in GitHub Desktop.
1 2
1 1
1 2
4 3
4 8
1 5
8 2
8 2
8 7
object Makelist {
val nums = """(\d+) (\d+)""".r
val tuples = io.Source.fromFile("list.txt").getLines collect {
case nums(label, num) => (label.toInt -> num.toInt)
}
def toList(tuples: Iterator[(Int, Int)]): Iterator[Seq[Int]] = {
if(!tuples.hasNext) Iterator.empty
else {
val (label, num) = tuples.next
val (succ, rest) = tuples.span(_._1 == label)
Iterator(num :: succ.map(_._2).toList) ++ toList(rest)
}
}
toList(tuples)foreach(println)
}
2,1,2
3,8
5
2,2,7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment