Skip to content

Instantly share code, notes, and snippets.

@klpx
Forked from tixxit/EditDistance.scala
Created April 21, 2016 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save klpx/8e12be3481274a3e3da8dbd983812e57 to your computer and use it in GitHub Desktop.
Save klpx/8e12be3481274a3e3da8dbd983812e57 to your computer and use it in GitHub Desktop.
Short Levenshtein distance implementation in Scala
package net.tixxit.levenshtein
import scala.math.min
object EditDistance {
def editDist[A](a: Iterable[A], b: Iterable[A]) =
((0 to b.size).toList /: a)((prev, x) =>
(prev zip prev.tail zip b).scanLeft(prev.head + 1) {
case (h, ((d, v), y)) => min(min(h + 1, v + 1), d + (if (x == y) 0 else 1))
}) last
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment