1.2.0 permalink Arrow_down_16x16
  • (-?> x form)
  • (-?> x form & forms)
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

1 Example top

  • ;; -?>> 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
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/->>

    Threads the expr through the forms. Inserts x as the last item in t

  • 0
    clojure.core/->

    Threads the expr through the forms. Inserts x as the second item in

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/contrib/core.clj:30 top

(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
   "
   -> -?>)
Vars in clojure.contrib.core/-?>: ->
Used in 0 other vars

Comments top

No comments for -?>. Log in to add a comment.