Skip to content

Commit

Permalink
method
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf committed Feb 19, 2020
1 parent 9e111a1 commit 4b853af
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions src/darkleaf/multidecorators.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@
(into (pop queue) tag-parents)))
(distinct acc))))

(defn- method [registry tag initial]
(let [tags (reversed-me-and-ancestors tag)]
(->> tags
(map registry)
(remove nil?)
(reduce (fn [acc decorator]
(fn [obj & args]
(apply decorator acc obj args)))
initial))))

(defn multi [dispatch initial]
(let [iregistry (atom {})]
(fn
Expand All @@ -28,15 +38,7 @@
:initial initial})
([obj & args]
(let [tag (apply dispatch obj args)
tags (reversed-me-and-ancestors tag)
reg @iregistry
f (reduce (fn [acc tag]
(if-some [decorator (reg tag)]
(fn [obj & args]
(apply decorator acc obj args))
acc))
initial
tags)]
f (method @iregistry tag initial)]
(apply f obj args))))))

(defn ^{:style/indent :defn} decorate [multi tag decorator]
Expand Down

0 comments on commit 4b853af

Please sign in to comment.