Skip to content

Instantly share code, notes, and snippets.

@number23
Created September 4, 2014 15:16
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 number23/ab35abdfc42e5c32885b to your computer and use it in GitHub Desktop.
Save number23/ab35abdfc42e5c32885b to your computer and use it in GitHub Desktop.
clojure ancestors fn
user=> (ancestors (class #{}))
#{clojure.lang.IFn java.lang.Runnable clojure.lang.AFn clojure.lang.Counted clojure.lang.IMeta clojure.lang.IHashEq clojure.lang.IObj clojure.lang.IEditableCollection java.util.Collection java.lang.Object clojure.lang.APersistentSet java.util.concurrent.Callable clojure.lang.IPersistentSet clojure.lang.IPersistentCollection clojure.lang.Seqable java.lang.Iterable java.io.Serializable java.util.Set}
user=> (ancestors (class ""))
#{java.lang.CharSequence java.lang.Comparable java.lang.Object java.io.Serializable}
user=> (doc ancestors)
-------------------------
clojure.core/ancestors
([tag] [h tag])
Returns the immediate and indirect parents of tag, either via a Java type
inheritance relationship or a relationship established via derive. h
must be a hierarchy obtained from make-hierarchy, if not supplied
defaults to the global hierarchy
nil
user=> (source ancestors)
(defn ancestors
"Returns the immediate and indirect parents of tag, either via a Java type
inheritance relationship or a relationship established via derive. h
must be a hierarchy obtained from make-hierarchy, if not supplied
defaults to the global hierarchy"
{:added "1.0"}
([tag] (ancestors global-hierarchy tag))
([h tag] (not-empty
(let [ta (get (:ancestors h) tag)]
(if (class? tag)
(let [superclasses (set (supers tag))]
(reduce1 into1 superclasses
(cons ta
(map #(get (:ancestors h) %) superclasses))))
ta)))))
nil
user=>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment