bevacqua / fuzzysearch

:crystal_ball: Tiny and blazing-fast fuzzy search in JavaScript
https://ponyfoo.com
MIT License
2.71k stars 87 forks source link

Interesing, and I got one too: #2

Closed tiye closed 9 years ago

tiye commented 9 years ago

Mine was in a functional one written in CirruScript(compiles with http://repo.cirru.org/script/):

= generateSearch $ \ (text query info index)
  -- "return if matches"
  if (is text query)
    do $ return $ object (:match false) (:start 0)
  -- "reurn false if text is using up"
  if (and (is text.length 0) (> query.length 0))
    do $ return $ object (:match false) (:start 0)
  -- "return true if query is using up"
  if (is query.length 0)
    do $ return info

  = nextIndex $ + index 1
  -- "first letter matches, keep going without first letters"
  if (is (. text 0) (. query 0))
    do
      if (< nextIndex info.start)
        do $ = info.start nextIndex
      return $ generateSearch (text.substr 1) (query.substr 1) info nextIndex
  -- "first letter does not match, but keep going without first letter of text"
  generateSearch (text.substr 1) query info nextIndex

= exports.fuzzyStart $ \ (list query)
  = result $ list.map $ \ (text)
    = info $ object (:start 10) (:match true) (:text text)
    generateSearch text query info 0
  -- "filter and sort"
  = result $ result.filter $ \ (item) item.match

  = result $ result.sort $ \ (a b)
    cond
      (< a.start b.start) -1
      (> a.start b.start) 1
      else 0

  return $ result.map $ \ (item) item.text
bevacqua commented 9 years ago

That language looks very strange..

tiye commented 9 years ago

Alright maybe I should only paste the compiled code here. I would prefer writing in a functional style anyway.