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