Skip to content

Instantly share code, notes, and snippets.

@sortega
Created January 5, 2014 22:30
Show Gist options
  • Save sortega/8274885 to your computer and use it in GitHub Desktop.
Save sortega/8274885 to your computer and use it in GitHub Desktop.
Imperative solution to the Python indentation problem
import scala.io.Source
import scala.collection.mutable
object ImperativeIndentationCheck {
def isValid(filename: String): Boolean = isValid(Source.fromFile(filename).getLines.toSeq)
def isValid(lines: Seq[String]): Boolean = {
val scopes = mutable.Stack(0)
for(line <- lines) {
val i = indentationOf(line)
if (i > scopes.head) {
scopes.push(i)
} else while (i != scopes.head) {
scopes.pop()
if (scopes.isEmpty) return false
}
}
return true
}
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