aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/main/clojure/clojure/contrib/core.clj8
-rw-r--r--src/test/clojure/clojure/contrib/test_core.clj14
2 files changed, 18 insertions, 4 deletions
diff --git a/src/main/clojure/clojure/contrib/core.clj b/src/main/clojure/clojure/contrib/core.clj
index fe6f50e9..c696ed12 100644
--- a/src/main/clojure/clojure/contrib/core.clj
+++ b/src/main/clojure/clojure/contrib/core.clj
@@ -43,6 +43,14 @@
"
.. .?.)
+(defnilsafe
+ "Same as clojure.core/->> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
+ Examples :
+ (-?>> (range 5) (map inc)) returns (1 2 3 4 5)
+ (-?>> [] seq (map inc)) returns nil
+ "
+ ->> -?>>)
+
;; ----------------------------------------------------------------------
;; scgilardi at gmail
diff --git a/src/test/clojure/clojure/contrib/test_core.clj b/src/test/clojure/clojure/contrib/test_core.clj
index a27d63ee..3048778c 100644
--- a/src/test/clojure/clojure/contrib/test_core.clj
+++ b/src/test/clojure/clojure/contrib/test_core.clj
@@ -25,12 +25,18 @@
(is (thrown? NullPointerException (.. [nil] (get 0) toString)))))
(deftest test-new-versions
- (testing "Version -?> returns nil if passed nil"
+ (testing "Version -?>> falls out on nil"
+ (is (nil? (-?>> nil .toString)))
+ (is (nil? (-?>> [] seq (map inc))))
+ (is (= [] (->> [] seq (map inc)))))
+ (testing "Version -?>> completes for non-nil"
+ (is (= [3 4] (-?>> [1 2] (map inc) (map inc)))))
+ (testing "Version -?> falls out on nil"
(is (nil? (-?> nil .toString)))
(is (nil? (-?> "foo" seq next next next .toString))))
- (testing "Version -?> works well for some basic use cases"
- (is (= (list \O \O) (-?> "foo" .toUpperCase rest))))
+ (testing "Version -?> completes for non-nil"
+ (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
+