While still learning clojure, I often times need a function and am not
sure if it already exists or where to look for it. I thought it would
be useful to be able to give a return value and arguments and search
all functions for ones which return the given value when called with
the given arguments. Here's what I came up with:
(defn test-for
"Tests to see if a given function called with given arguments
results
in given value, catching any exceptions that occur"
[v f & args]
(try (= v (apply f args))
(catch Exception e nil)))
(defn find-func
"Given a value and arguments, searches all public functions in all
namespaces"
[v & args]
(map key
(mapcat
(fn [ns_]
(filter
#(binding [*out* nil *in* nil]
(apply test-for v (val %) args))
(ns-publics (the-ns ns_))))
(all-ns))))
Binding *in* and *out* stops any IO functions from messing up the
search. There's probably still ways this could be dangerous though,
depending on the given args. Hope someone finds this useful or
improves upon it.
-Mitch
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---