Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, 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
View
9 src/dojo/handler.clj
@@ -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 []
View
8 test/dojo/handler_test.clj
@@ -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.