clojure - How to simplify those tow macro when runtime type depend? -


guys, @ code first:

(defmacro map-remove- [v w]   `(dosync      (ref-set ~v (dissoc @~v (keyword ~w))))) (defmacro set-remove- [v w]   `(dosync      (ref-set ~v (disj @~v ~w)))) (defmacro clean- [v]    `(dosync     (ref-set ~v (empty @~v)))) 

they work fine , want write more general macro combine "map-remove-" , "set-remove-" in one. according c/java experience chose "case" case can't use in macro defination cause "the test-constants not evaluated. must compile-time literals", following code won't work:

(defmacro [x]   (case (type x) ;;;;;;;;;;; never work!directly reach default clause      ....))  

anybody has suggestion? appreciate.

you can use functions map? , set? test whether value map or set respectively.

i'm not sure have enough perspective on you're trying here though - i've substituted macro instead of function, because can't see necessity macro in sample you've given.

;; create function remove in different way when given map/set , string (defn remove- [v w]   (dosync      (cond          (map? @v)         (ref-set v (dissoc @v (keyword w)))          (set? @v)         (ref-set v (disj @v w)))))   ;; set mutable refs  (def m (ref {:a 1 :b 2}))  (def s (ref #{"a" "b" "c"}))   ;; remove refs  (remove- m "b") => {:a 1}  (remove- s "b") => #{"a" "c"} 

on side note - sure need use refs? know coming c/java background, mutability default, i've never had use in clojure far. clojure places lot of emphasis on immutability, , things can done (often elegantly) using functions on immutable values.


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -