Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation).
Examples :
(-?> "foo" .toUpperCase (.substring 1)) returns "OO"
(-?> nil .toUpperCase (.substring 1)) returns nil
;; -?>> can be used if intermediate forms may potentially return nil. ;; This is to prevent user=> (use '[clojure.contrib.core :only (-?>>)]) nil ;; create function that will return nil if the passed value is "Hello" user=> (defn return-nil-for-hello [str-value] (if (= str-value "Hello") nil str-value)) #'user/return-nil user=> (->> "Greetings" return-nil-for-hello .toUpperCase) "GREETINGS" ;; null pointer exception is thrown because return-nil-for-hello ;; returns nil and method .toUpperCase cannot process nil user=> (->> "Hello" return-nil-for-hello .toUpperCase) NullPointerException clojure.lang.Reflector.invokeNoArgInstanceMember (Reflector.java:314) ;; using -?>> will prevent null exception thrown user=> (-?>> "Hello" return-nil-for-hello .toUpperCase) nil
(defnilsafe "Same as clojure.core/-> but returns nil as soon as the threaded value is nil itself (thus short-circuiting any pending computation). Examples : (-?> \"foo\" .toUpperCase (.substring 1)) returns \"OO\" (-?> nil .toUpperCase (.substring 1)) returns nil " -> -?>)
Comments top
No comments for -?>. Log in to add a comment.