• (doto x & forms)
Evaluates x then calls all of the methods and functions with the
value of x supplied at the front of the given arguments. The forms
are evaluated in order. Returns x.

(doto (new java.util.HashMap) (.put "a" 1) (.put "b" 2))

2 Examples top

  • ;; Note that even though println returns nil, doto still returns the date object
    user> (doto (java.util.HashMap.)
                (.put "a" 1)
                (.put "b" 2)
                (println :printed))
    #<HashMap {b=2, a=1}> :printed
    #<HashMap {b=2, a=1}>
  • ;; quick demonstration of using a Collections function on the resulting ArrayList
    
    user=> (def al (doto (java.util.ArrayList.) (.add 11) (.add 3)(.add 7)))
    #'user/al
    user=> al
    #<ArrayList [11, 3, 7]>
    user=> (java.util.Collections/sort al)
    nil
    user=> al
    #<ArrayList [3, 7, 11]>
    user=>
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:3395 top

(defmacro doto
  "Evaluates x then calls all of the methods and functions with the
  value of x supplied at the front of the given arguments.  The forms
  are evaluated in order.  Returns x.

  (doto (new java.util.HashMap) (.put \"a\" 1) (.put \"b\" 2))"
  {:added "1.0"}
  [x & forms]
    (let [gx (gensym)]
      `(let [~gx ~x]
         ~@(map (fn [f]
                  (if (seq? f)
                    `(~(first f) ~gx ~@(next f))
                    `(~f ~gx)))
                forms)
         ~gx)))
Vars in clojure.core/doto:
Used in 0 other vars

Comments top

No comments for doto. Log in to add a comment.