Skip to content

Instantly share code, notes, and snippets.

@add20
Last active December 31, 2015 17:39
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 add20/8021618 to your computer and use it in GitHub Desktop.
Save add20/8021618 to your computer and use it in GitHub Desktop.
SublimeTextのfuzzy search algorithmをHaskellで書いてみた。 参考:http://www.quora.com/Algorithms/How-is-the-fuzzy-search-algorithm-in-Sublime-Text-designed
module FuzzyMatch where
import Data.Char (toLower)
-- $setup
-- import Data.List (sort)
-- >>> let searchSet = ["Strings to search, one per line", "diff_match_patch.js", "diff_match_patch_uncompressed.js", "diff_match_patch_test.js"]
-- |
-- >>> Data.List.sort $ fuzzyMatch searchSet "mas"
-- ["diff_match_patch.js","diff_match_patch_test.js","diff_match_patch_uncompressed.js"]
--
-- >>> Data.List.sort $ fuzzyMatch searchSet "mau"
-- ["diff_match_patch_uncompressed.js"]
fuzzyMatch :: [String] -> String -> [String]
fuzzyMatch ws query = filter (isMatch (map toLower query)) ws
where
-- |
-- >>> isMatch "ms" "match"
-- False
--
-- >>> isMatch "ms" "mouse"
-- True
isMatch :: String -> String -> Bool
isMatch [] _ = True
isMatch _ [] = False
isMatch query'@(q:qs) (x:xs)
| q == toLower x = isMatch qs xs
| otherwise = isMatch query' xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment