Skip to content

Instantly share code, notes, and snippets.

@netologist
Last active December 17, 2015 11:19
Show Gist options
  • Save netologist/5601514 to your computer and use it in GitHub Desktop.
Save netologist/5601514 to your computer and use it in GitHub Desktop.
// 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