diff options
author | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-12 22:01:58 -0400 |
---|---|---|
committer | Stuart Halloway <stu@thinkrelevance.com> | 2010-04-12 22:01:58 -0400 |
commit | 9f448420b082ce8a6e577696df5203a405f4e8d4 (patch) | |
tree | 959fd9277daa1d73194bd905bf9949323e2eda21 | |
parent | 6dd033d9e12337f6630faa3d3f5c2e901a28c4f4 (diff) |
-?>> per Chas Emerick
-rw-r--r-- | src/main/clojure/clojure/contrib/core.clj | 8 | ||||
-rw-r--r-- | src/test/clojure/clojure/contrib/test_core.clj | 14 |
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 + |