私はScalaの初心者です。トルコの市民権番号検証アルゴリズムを試しました。
このscalaコードを実装して最適化するにはどうすればよいですか?
私のJavaバージョンは、このリンクhttps://gist.github.com/hasanozgan/5601623で見つけることができます
trait TurkishCitizenshipNumberValidator {
private def odd(tckn: String): Int = {
tckn.zipWithIndex.foldLeft(0) {
(total, x) =>
x match {
case i if ((i._2 % 2 == 0 && i._2 < 10)) => ((i._1.asDigit) + total)
case _ => total
}
}
}
private def even(tckn: String): Int = {
tckn.zipWithIndex.foldLeft(0) {
(total, x) =>
x match {
case i if ((i._2 % 2 == 1) && i._2 < 9) => ((i._1.asDigit) + total)
case _ => total
}
}
}
private def total(tckn: String): Int = {
tckn.zipWithIndex.foldLeft(0) {
(total, x) =>
x match {
case i if (i._2 < 10) => ((i._1.asDigit) + total)
case _ => total
}
}
}
def turkishCitizenshipNumberValidator(t: String): Boolean = {
val digit10 = total(t) % 10
val digit9 = ((odd(t) * 7) - even(t)) % 10
((t(9).asDigit == digit9) && t(10).asDigit == digit10)
}
}
object test extends TurkishCitizenshipNumberValidator {
// http://tckimliknouretici.appspot.com/
turkishCitizenshipNumberValidator("29419391592")
//> res0: Boolean = true
}