Skip to content

Instantly share code, notes, and snippets.

@DarkWiiPlayer
Created February 27, 2018 09:05
Show Gist options
  • Save DarkWiiPlayer/f00633a8333f11b3ec1d97edbab2481c to your computer and use it in GitHub Desktop.
Save DarkWiiPlayer/f00633a8333f11b3ec1d97edbab2481c to your computer and use it in GitHub Desktop.
Levenshtein distance in moonscript
import min from math
levenshtein = (a, b) ->
c = {nil, nil, nil, nil, nil}
p = {nil, nil, nil, nil, nil}
for i=1,#a+1
c[i]=i-1 -- Distance to ""
for i=1,#b
p,c = c,p
c[1] = i -- Distance from ""
for j=1,#a
deletioncost = p[j+1]+1
insertioncost = c[j]+1
local substitutioncost
if a\byte(j) == b\byte(i)
substitutioncost = p[j]
else
substitutioncost = p[j] + 1
c[j+1] = min(deletioncost, insertioncost, substitutioncost)
c[#a+1]
{:levenshtein}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment