contrib proposal: apropos

0 views
Skip to first unread message

Michel Salim

unread,
Jun 18, 2009, 1:57:49 PM6/18/09
to Clojure
Would anyone be interested in this? It takes either a string or a
symbol, and examines all namespaces for identifiers that match the
given input.

(defn substring? [str1 str2]
(let [c1 (count str1)
c2 (count str2)]
(and (<= c1 c2)
(or (= str1 (subs str2 0 c1))
(substring? str1
(subs str2 1 c2))))))

(defn apropos [kw]
(let [kwstr (str kw)]
(apply concat
(map (fn [ns]
(filter #(substring? kwstr (str %))
(keys (ns-publics ns))))
(all-ns)))))

Not sure what files in contrib to put these in -- and whether
substring? will be generally useful, and thus should be exported, or
not.

Thanks,

--
Michel Salim

Richard Newman

unread,
Jun 18, 2009, 3:45:41 PM6/18/09
to clo...@googlegroups.com
>
> (defn substring? [str1 str2]
> (let [c1 (count str1)
> c2 (count str2)]
> (and (<= c1 c2)
> (or (= str1 (subs str2 0 c1))
> (substring? str1
> (subs str2 1 c2))))))

This should be a little faster:

(defn substring? [#^String s1 #^String s2]
(not (= -1 (.indexOf s2 s1))))

user=> (substring? "foo" "foobar")
true
user=> (substring? "bar" "Baz")
false

Michel Salim

unread,
Jun 18, 2009, 5:34:27 PM6/18/09
to Clojure
Ah, true -- I've not used Java's string API for a while, and forgot
about indexOf. Thanks.

--
Michel
Reply all
Reply to author
Forward
0 new messages