Skip to content

Instantly share code, notes, and snippets.

@riccardomerolla
Last active December 26, 2015 01:29
Show Gist options
  • Save riccardomerolla/7071257 to your computer and use it in GitHub Desktop.
Save riccardomerolla/7071257 to your computer and use it in GitHub Desktop.
Scala file splitter/group by keyword
//val lines = scala.io.Source.fromFile("test.txt", "utf-8").getLines.toList
val lines = List("HEAD 000001","HEAD 000002","HEAD 000002","HEAD 000002","HEAD 000003","HEAD 000003")
val keywords = lines.map(x => x.substring(9, 15)).distinct
def groupByKeyword(lines: List[String], keywords: List[String]): List[List[String]] = keywords match {
case keyword :: Nil => lines.groupBy(x => x.substring(9, 15).contains(keyword)).get(true).toList
case keyword :: _keywords => {
val mapped = lines.groupBy(x => x.contains(keyword))
mapped.get(true).toList.head :: groupByKeyword(mapped.get(false).toList.head, _keywords)
}
case _ => Nil
}
@riccardomerolla
Copy link
Author

val res1 = groupByKeyword(lines, keywords)

Print sub lists size

res1.foreach(x => println (x.size))

Sum sub lists size

res1.map(x => x.size).sum

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment