diff --git a/src/net/cgrand/xforms.cljc b/src/net/cgrand/xforms.cljc index 82135e0..20f165d 100644 --- a/src/net/cgrand/xforms.cljc +++ b/src/net/cgrand/xforms.cljc @@ -28,8 +28,10 @@ (unreduced-> (-> x# ~expr) ~@exprs))))) (defn- pair? [x] (and (vector? x) (= 2 (core/count x)))) -(defn- destructuring-pair? [x] - (and (pair? x) (not (or (keyword? x) (= '& x))))) +(let [kw-or-& #(or (keyword? %) (= '& %))] + (defn- destructuring-pair? [x] + (and (pair? x) + (not (kw-or-& (first x)))))) (defmacro for "Like clojure.core/for with the first expression being replaced by % (or _). Returns a transducer. diff --git a/test/net/cgrand/xforms_test.cljc b/test/net/cgrand/xforms_test.cljc index 5280cf3..9de3e74 100644 --- a/test/net/cgrand/xforms_test.cljc +++ b/test/net/cgrand/xforms_test.cljc @@ -142,3 +142,16 @@ (is (= (reverse (range 100)) (x/into [] (x/sort >) (shuffle (range 100))))) (is (= (sort-by str (range 100)) (x/into [] (x/sort-by str) (shuffle (range 100))))) (is (= (sort-by str (comp - compare) (range 100)) (x/into [] (x/sort-by str (comp - compare)) (shuffle (range 100)))))) + +(deftest destructuring-pair? + (let [destructuring-pair? #'x/destructuring-pair?] + (are [candidate expected] + (= expected (destructuring-pair? candidate)) + '[a b] true + '[a b c] false + '[& foo] false + '[:as foo] false + 1 false + '(a b) false + '{foo bar} false + '{foo :bar} false)))