Skip to content

Instantly share code, notes, and snippets.

@sungkmi
Last active August 29, 2015 14:05
Show Gist options
  • Save sungkmi/b348b427522eedbe88d9 to your computer and use it in GitHub Desktop.
Save sungkmi/b348b427522eedbe88d9 to your computer and use it in GitHub Desktop.
object IgnoreAllMyComments extends App {
def ignoreComments(in: Iterator[Char])(out: Char => Unit): Unit =
((0, None) /:[(Int, Option[Char])] in) { case ((n, last), current) =>
last match {
case None => (n, Some(current))
case Some(ch) => ""+ch+current match {
case "/*" => (n+1, None)
case "*/" if n>0 => (n-1, None)
case _ =>
if (n==0) out(ch)
(n, Some(current))
}
}
} match {
case (0, Some(ch)) => out(ch)
case _ =>
}
val writer = new java.io.PrintWriter("e.small.out")
try {
writer.println("Case #1:")
ignoreComments(io.Source.fromFile("E-small-practice.in").iter)(writer.print)
} finally {
writer.flush(); writer.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment