All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.halfmatthalfcat.stringmetric.similarity.HammingMetric.scala Maven / Gradle / Ivy

package com.github.halfmatthalfcat.stringmetric.similarity

import com.github.halfmatthalfcat.stringmetric.{CompareTuple, StringMetric}

case object HammingMetric extends StringMetric[Int] {
	override def compare(a: Array[Char], b: Array[Char]): Option[Int] =
		if (a.length == 0 || b.length == 0 || a.length != b.length) None
		else if (a.sameElements(b)) Some(0)
		else Some(hamming(a, b))

	override def compare(a: String, b: String): Option[Int] = compare(a.toCharArray, b.toCharArray)

	private val hamming: (CompareTuple[Char] => Int) = (ct) =>
		if (ct._1.length == 0) 0
		else ct._1.zip(ct._2).count(t => t._1 != t._2)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy