Skip to content

Instantly share code, notes, and snippets.

@sortega
Created January 5, 2014 22:32
Show Gist options
  • Save sortega/8274924 to your computer and use it in GitHub Desktop.
Save sortega/8274924 to your computer and use it in GitHub Desktop.
Functional solution to the Python indentation problem check
import scala.io.Source
object IndentationCheck {
def isValid(filename: String): Boolean = isValid(Source.fromFile(filename).getLines.toSeq)
def isValid(lines: Seq[String]): Boolean = {
val initialAccum: Option[Set[Int]] = Some(Set(0))
lines.map(indentationOf).foldLeft(initialAccum) { (accum, i) =>
accum match {
case Some(history) if i >= history.max => Some(history + i)
case Some(history) if i < history.max && history.contains(i) => Some(history.filter(_ <= i))
case _ => None
}
}.isDefined
}
private def indentationOf(line: String) = line.takeWhile(_.isWhitespace).size
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment