Created
January 5, 2014 22:32
-
-
Save sortega/8274924 to your computer and use it in GitHub Desktop.
Functional solution to the Python indentation problem check
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
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