Skip to content

Commit

Permalink
-?>> per Chas Emerick
Browse files Browse the repository at this point in the history
  • Loading branch information
stuarthalloway committed Apr 13, 2010
1 parent 6dd033d commit 9f44842
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/main/clojure/clojure/contrib/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 10 additions & 4 deletions src/test/clojure/clojure/contrib/test_core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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.