Last active
December 17, 2015 11:19
-
-
Save netologist/5601514 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
// http://stackoverflow.com/questions/16617003/how-do-i-short-this-scala-code | |
trait TurkishCitizenshipNumberValidator { | |
def turkishCitizenshipNumberValidator(t: String): Boolean = { | |
if (t.length != 11 || !t.forall(_.isDigit)) false | |
else { | |
val n = t.map(_.asDigit) | |
val evens = n.grouped(2).take(5).map(_(0)).sum | |
val odds = n.grouped(2).take(4).map(_(1)).sum | |
n(10) == (n.take(10).sum % 10) && n(9) == ((evens * 7 - odds) % 10) | |
} | |
} //> turkishCitizenshipNumberValidator: (t: String)Boolean | |
} | |
object test extends TurkishCitizenshipNumberValidator { | |
// http://tckimliknouretici.appspot.com/ | |
turkishCitizenshipNumberValidator("29419391592") | |
//> res0: Boolean = true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment