Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. dylon revised this gist Dec 9, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion multimethods-under-varying-relationship-hierarchies.clj
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    (def h (atom (make-hierarchy)))

    (reset! h (derive @h ::a ::b))
    (swap! h derive ::a ::b)

    ;-> "(isa? user/a user/b), under the global hierarchy: false"
    (println "(isa? user/a user/b), under the global hierarchy:" (isa? ::a ::b))
  2. dylon renamed this gist Dec 9, 2013. 1 changed file with 0 additions and 0 deletions.
  3. dylon created this gist Dec 9, 2013.
    29 changes: 29 additions & 0 deletions Multimethods under varying, relationship hierarchies
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    (def h (atom (make-hierarchy)))

    (reset! h (derive @h ::a ::b))

    ;-> "(isa? user/a user/b), under the global hierarchy: false"
    (println "(isa? user/a user/b), under the global hierarchy:" (isa? ::a ::b))

    ;-> "(isa? user/a user/b), under the hierarchy, h: true"
    (println "(isa? user/a user/b), under the hierarchy, h:" (isa? @h ::a ::b))

    (defmulti f identity :hierarchy h :default :unmatched)
    (defmethod f ::b [x]
    (format "I, %s, isa? user/b under f!" (print-str x)))
    (defmethod f :unmatched [x]
    (format "I, %s, am sorrowfully-not isa? user/b under f..." (print-str x)))

    (println (f ::b)) ;-> "I, user/b, isa? user/b under f!"
    (println (f ::a)) ;-> "I, user/a, isa? user/b under f!"
    (println (f ::c)) ;-> "I, user/c, am sorrowfully-not isa? user/b under f ..."

    (defmulti g identity)
    (defmethod g ::b [x]
    (format "I, %s, isa? :user/b under g!" (print-str x)))
    (defmethod g :default [x]
    (format "I, %s, am sorrowfully-not isa? :user/b under g ..." (print-str x)))

    (println (g ::b)) ;-> "I, user/b, isa? user/b under g!"
    (println (g ::a)) ;-> "I, user/a, am sorrowfully-not isa? user/b under g ..."
    (println (g ::c)) ;-> "I, user/c, am sorrowfully-not isa? user/b under g ..."