A technique that works for me is to create sequences which are
"augmented"
with properties and then filter and transform those sequences in a
kind of pipeline.
Using that approach I came up with the following:
(defn eval-candidate [needle candidate]
"Returns a map representing derived information about the candidate
vector and its
fit with the search target (needle) vector"
{:score (reduce + (map #(if (= %1 %2) 1 0) needle candidate))
:len (count candidate)
:vec candidate}
)
(defn closest-match [needle haystack]
"Tries to find the best fit for the given needle vector in the
haystack of candidate vectors"
(let [augmented (map (partial eval-candidate needle) haystack)
max-score (apply max (map :score augmented))]
(if (= max-score 0) ; no good matches
at all
[]
(let [candidates (filter #(= max-score (:score %)) augmented)]
(if (> (count candidates) 1) ; a single match wins
(let [min-length (apply min (map :len candidates))
winners (filter #(= min-length (:len %)) candidates)]
(map :vec winners) )
(map :vec candidates)
) ) ) )
)
cheers,
-tom
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en