Skip to content

Instantly share code, notes, and snippets.

@yoshiyoshifujii
Last active January 20, 2018 00:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yoshiyoshifujii/b29a5a891662bfa8429f3e4cc9d42144 to your computer and use it in GitHub Desktop.
Save yoshiyoshifujii/b29a5a891662bfa8429f3e4cc9d42144 to your computer and use it in GitHub Desktop.
import scala.collection.immutable.TreeMap
import scala.io.Source
object Main extends App {
val file = Source.fromFile("./hoge.txt", "utf-8")
val lines = file.getLines
val dateTimeSep = """- (\d\d\d\d)年(\d\d)月(\d\d)日 .曜日 (\d\d:\d\d)""".r
val articles = lines.foldLeft("" -> TreeMap.empty[String, String]) { (acc, line) =>
val (marker, treeMap) = acc
line match {
case dateTimeSep(year, month, day, time) =>
val nowDate = s"$year-$month-$day $time"
nowDate -> treeMap.+(nowDate -> "")
case _ =>
val data = treeMap.getOrElse(marker, "") + line + "\n"
marker -> treeMap.+(marker -> data)
}
}
println(articles)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment