Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: ndpar/clojure
base: 4bf1b75c95
...
head fork: ndpar/clojure
compare: 46148ef439
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Commits on Nov 24, 2012
Andrey Paramonov Replaced interleave with mapcat c05e663
Commits on Nov 25, 2012
Andrey Paramonov Fixed variable capture 46148ef
Showing with 13 additions and 4 deletions.
  1. +5 −4 src/dojo/handler.clj
  2. +8 −0 test/dojo/handler_test.clj
9 src/dojo/handler.clj
View
@@ -1,10 +1,11 @@
(ns dojo.handler)
(defmacro handler [name args & body]
- `(fn [~'msg]
- (let [~@(interleave args (map (fn [x] `(get ~'msg ~(keyword x))) args))]
- (when (or ~@args)
- ~@body))))
+ (let [msg (gensym)]
+ `(fn [~msg]
+ (let [~@(mapcat (fn [x] [x `(get ~msg ~(keyword x))]) args)]
+ (when (or ~@args)
+ ~@body)))))
(defmacro build-handlers [& body]
`(defn- handlers []
8 test/dojo/handler_test.clj
View
@@ -8,3 +8,11 @@
(is (= ["Document:A:null" "Note:B:null" "Alert:A:B"] (on-message {:id 1 :a "A" :b "B"})))
(is (= ["Document:A:C" "Note:null:C" "Alert:A:null"] (on-message {:id 2 :a "A" :c "C"})))
(is (= ["Document:A:C" "Note:B:C" "Alert:A:B"] (on-message {:id 2 :a "A" :b "B" :c "C"}))))
+
+(deftest var-capturing-test
+ (let [msg-handler (handler msg [msg something]
+ (format "Msg:%s:%s" msg something))
+ x-handler (handler x [x something]
+ (format "X:%s:%s" x something))]
+ (is (= "Msg:3:5" (msg-handler {:msg 3 :something 5})))
+ (is (= "X:3:5" (x-handler {:x 3 :something 5})))))

No commit comments for this range

Something went wrong with that request. Please try again.