Created
November 29, 2012 05:40
-
-
Save takkkun/4167033 to your computer and use it in GitHub Desktop.
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.util.matching.Regex | |
class StringValidator(strings: List[String]) { | |
var or = false | |
var not = false | |
var result = true | |
def judge = result | |
def and = self(or = false) | |
def matched(regex: Regex) = self { | |
evaluate(string => regex.findFirstIn(string) match { | |
case None => false | |
case _ => true | |
}) | |
} | |
private def evaluate(validate: String => Boolean) { | |
if (!(or ^ result)) return | |
result = strings.forall(string => validate(string) ^ not) | |
not = false | |
} | |
private def self(process: => Unit) = { | |
process | |
this | |
} | |
} | |
object StringValidator { | |
implicit def stringToStringValidator(string: String) = new StringValidator(List(string)) | |
implicit def listToStringValidator(list: List[String]) = new StringValidator(list) | |
implicit def stringValidatorToBoolean(stringValidator: StringValidator) = stringValidator.judge | |
} | |
object Main { | |
import StringValidator._ | |
def main(args: Array[String]) { | |
val strings = args.toList | |
val result = if (strings.matched("^nya".r).and.matched("nya$".r)) "true" else "false" | |
println(result) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment