Permalink
Browse files

-?>> per Chas Emerick

  • Loading branch information...
1 parent 6dd033d commit 9f448420b082ce8a6e577696df5203a405f4e8d4 @stuarthalloway stuarthalloway committed Apr 13, 2010
Showing with 18 additions and 4 deletions.
  1. +8 −0 src/main/clojure/clojure/contrib/core.clj
  2. +10 −4 src/test/clojure/clojure/contrib/test_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
@@ -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)))))
-
+

0 comments on commit 9f44842

Please sign in to comment.