Last active
December 26, 2015 01:29
-
-
Save riccardomerolla/7071257 to your computer and use it in GitHub Desktop.
Scala file splitter/group by keyword
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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