File tree Expand file tree Collapse file tree 3 files changed +48
-4
lines changed Expand file tree Collapse file tree 3 files changed +48
-4
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,13 @@ Like multimethods but multidecorators.
59
59
(assert (= [] (func ::f )))
60
60
```
61
61
62
+ ## Memoization
63
+
64
+ ``` clojure
65
+ (defn -main []
66
+ (alter-var-root #'func md/memoize-multi))
67
+ ```
68
+
62
69
## Development
63
70
64
71
```
Original file line number Diff line number Diff line change 41
41
f (method @iregistry tag initial)]
42
42
(apply f obj args))))))
43
43
44
+ (defn memoize-multi [multi]
45
+ (case (:type (multi ))
46
+ :memoized multi
47
+ :dynamic (let [{:keys [iregistry
48
+ dispatch
49
+ initial]} (multi )
50
+ registry @iregistry
51
+ mem-method (memoize method)]
52
+ (fn
53
+ ([] {:type :memoized
54
+ :registry registry
55
+ :initial initial
56
+ :dispatch dispatch})
57
+ ([obj & args]
58
+ (let [tag (apply dispatch obj args)
59
+ f (mem-method registry tag initial)]
60
+ (apply f obj args)))))))
61
+
44
62
(defn ^{:style/indent :defn } decorate [multi tag decorator]
45
- (let [state (multi )
46
- iregistry (:iregistry state)]
47
- (swap! iregistry assoc tag decorator)
48
- multi))
63
+ (case (:type (multi ))
64
+ :dynamic (let [state (multi )
65
+ iregistry (:iregistry state)]
66
+ (swap! iregistry assoc tag decorator)
67
+ multi)))
Original file line number Diff line number Diff line change 33
33
(t/is (= [] (multi ::f )))
34
34
(t/is (= [:a :b :c :d 's] (multi `s)))
35
35
#?(:clj (t/is (= [:a :b :c :d :obj ] (multi String))))))
36
+
37
+ (t/deftest memoization
38
+ (let [multi (doto (md/multi identity (constantly []))
39
+ (md/decorate ::a (fn [super obj] (conj (super obj) :a )))
40
+ (md/decorate ::b (fn [super obj] (conj (super obj) :b )))
41
+ (md/decorate ::c (fn [super obj] (conj (super obj) :c )))
42
+ (md/decorate ::d (fn [super obj] (conj (super obj) :d )))
43
+ (md/decorate `s (fn [super obj] (conj (super obj) 's)))
44
+ #?(:clj (md/decorate Object (fn [super obj] (conj (super obj) :obj )))))
45
+ mem-multi (md/memoize-multi multi)]
46
+ (doseq [_ (range 2 )]
47
+ (t/is (= [:a ] (mem-multi ::a )))
48
+ (t/is (= [:a :b ] (mem-multi ::b )))
49
+ (t/is (= [:a :c ] (mem-multi ::c )))
50
+ (t/is (= [:a :b :c :d ] (mem-multi ::d )))
51
+ (t/is (= [] (mem-multi ::f )))
52
+ (t/is (= [:a :b :c :d 's] (mem-multi `s)))
53
+ #?(:clj (t/is (= [:a :b :c :d :obj ] (mem-multi String)))))))
You can’t perform that action at this time.
0 commit comments