Created
January 19, 2014 21:06
-
-
Save szensk/8511006 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--fuzzy.moon | |
matches = | |
["^"]: "%^", | |
["$"]: "%$", | |
["("]: "%(", | |
[")"]: "%)", | |
["%"]: "%%", | |
["."]: "%.", | |
["["]: "%[", | |
["]"]: "%]", | |
["*"]: "%*", | |
["+"]: "%+", | |
["-"]: "%-", | |
["?"]: "%?", | |
["\0"]: "%z" | |
escape_lua_pattern = (s) -> | |
(s\gsub(".", matches)) | |
str = { | |
"adv", | |
"advent.timer", | |
"advent", | |
"aldav", | |
"parity", | |
"parity.timer" | |
} | |
pattern = nil | |
set_pattern = (pat) -> | |
pat = escape_lua_pattern(pat) | |
pattern = {""} | |
for i=1, #pat do pattern[i+1] = pat\sub(i,i) | |
pattern[#pattern + 1] = "" | |
pattern = table.concat(pattern,"(.-)") | |
score = (str) -> | |
s, e = str\find(pattern) | |
return 0 unless s | |
e = s if s > e | |
return 100 / (s * (e - s + 1)) | |
set_pattern("av") | |
table.sort str, (a,b) -> | |
sa, sb = score(a), score(b) | |
if sa == sb | |
#a < #b | |
else | |
sa > sb | |
for s in *str do print s, score(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment