From b54dbbdbee2fafa8ba072878df6f300777a228c5 Mon Sep 17 00:00:00 2001
From: Herwig Hochleitner <hhochleitner@gmail.com>
Date: Mon, 3 Dec 2012 18:27:15 +0100
Subject: [PATCH] CINCU-3: Implement and test apply-kw

---
 src/main/clojure/clojure/core/incubator.clj      |   10 +++++++++-
 src/test/clojure/clojure/core/incubator_test.clj |   14 +++++++++++++-
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/src/main/clojure/clojure/core/incubator.clj b/src/main/clojure/clojure/core/incubator.clj
index 40154f1..4fda3e6 100644
--- a/src/main/clojure/clojure/core/incubator.clj
+++ b/src/main/clojure/clojure/core/incubator.clj
@@ -83,4 +83,12 @@
       (instance? Iterable x)
       (-> x .getClass .isArray)
       (string? x)
-      (instance? java.util.Map x)))
\ No newline at end of file
+      (instance? java.util.Map x)))
+
+(defn apply-kw
+  "Like apply, but f takes keyword arguments and the last argument is
+  not a seq but a map with the arguments for f"
+  [f & args]
+  {:pre [(map? (last args))]}
+  (apply f (apply concat
+                  (butlast args) (last args))))
diff --git a/src/test/clojure/clojure/core/incubator_test.clj b/src/test/clojure/clojure/core/incubator_test.clj
index 5feb327..a1708cd 100644
--- a/src/test/clojure/clojure/core/incubator_test.clj
+++ b/src/test/clojure/clojure/core/incubator_test.clj
@@ -34,4 +34,16 @@
     (is (= [\O \O] (-?> "foo" .toUpperCase rest))))
   (testing "Version .?. returns nil if one of the intermediate threaded values is nil"
     (is (nil? (.?. nil toString)))
-    (is (nil? (.?. [nil] (get 0) toString)))))
\ No newline at end of file
+    (is (nil? (.?. [nil] (get 0) toString)))))
+
+(deftest test-apply-kw
+  (let [kwargs {:karg1 "karg1" :karg2 "karg2"}]
+    (is (= ["a1" "karg2" kwargs]
+           (apply-kw (fn [arg1 & {:keys [karg2] :as kws}]
+                       [arg1 karg2 kws])
+                     "a1" kwargs)))
+    (is (= ["karg1" "karg2"]
+           (apply-kw (fn [& {:keys [karg1 karg2]}]
+                       [karg1 karg2])
+                     kwargs)))
+    (is (thrown? AssertionError (apply-kw (fn []) "foo")))))
-- 
1.7.8.6

