1.3.0 permalink Arrow_down_16x16

doseq

clojure.core

  • (doseq seq-exprs & body)
Repeatedly executes body (presumably for side-effects) with
bindings and filtering as provided by "for". Does not retain
the head of the sequence. Returns nil.

5 Examples top

  • user=> (doseq [x [1 2 3] 
                   y [1 2 3]] 
             (prn (* x y)))
    1
    2
    3
    2
    4
    6
    3
    6
    9
    nil
    
  • user=> (doseq [[x y] (map list [1 2 3] [1 2 3])] 
             (prn (* x y)))
    1
    4
    9
    nil
    
    ;; where
    user=> (map list [1 2 3] [1 2 3])
    ((1 1) (2 2) (3 3))
  • user=> (doseq [[[a b] [c d]] (map list {:1 1 :2 2} {:3 3 :4 4})] 
             (prn (* b d)))
    3
    8
    nil
    
    ;; where
    user=> (map list {:1 1 :2 2} {:3 3 :4 4})
    (([:1 1] [:3 3]) ([:2 2] [:4 4]))
  • user=> (doseq [[k v] (map identity {:1 1 :2 2 :3 3})] 
             (prn k v))
    :1 1
    :2 2
    :3 3
    nil
    
    ;; where
    user=> (map identity {:1 1 :2 2 :3 3})
    ([:1 1] [:2 2] [:3 3])
    
    ;; or simply
    user=> (doseq [[k v] {:1 1 :2 2 :3 3}]
             (prn k v))
    :1 1
    :3 3
    :2 2
    nil
  • ; Multiple sequences results in a Cartesian cross of their values.
    user=> (doseq [a [1 2]
                   b [3 4]]
             (println a b))
    1 3
    1 4
    2 3
    2 4
    nil
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:2797 top

(defmacro doseq
  "Repeatedly executes body (presumably for side-effects) with
  bindings and filtering as provided by \"for\".  Does not retain
  the head of the sequence. Returns nil."
  {:added "1.0"}
  [seq-exprs & body]
  (assert-args doseq
     (vector? seq-exprs) "a vector for its binding"
     (even? (count seq-exprs)) "an even number of forms in binding vector")
  (let [step (fn step [recform exprs]
               (if-not exprs
                 [true `(do ~@body)]
                 (let [k (first exprs)
                       v (second exprs)]
                   (if (keyword? k)
                     (let [steppair (step recform (nnext exprs))
                           needrec (steppair 0)
                           subform (steppair 1)]
                       (cond
                         (= k :let) [needrec `(let ~v ~subform)]
                         (= k :while) [false `(when ~v
                                                ~subform
                                                ~@(when needrec [recform]))]
                         (= k :when) [false `(if ~v
                                               (do
                                                 ~subform
                                                 ~@(when needrec [recform]))
                                               ~recform)]))
                     (let [seq- (gensym "seq_")
                           chunk- (with-meta (gensym "chunk_")
                                             {:tag 'clojure.lang.IChunk})
                           count- (gensym "count_")
                           i- (gensym "i_")
                           recform `(recur (next ~seq-) nil 0 0)
                           steppair (step recform (nnext exprs))
                           needrec (steppair 0)
                           subform (steppair 1)
                           recform-chunk 
                             `(recur ~seq- ~chunk- ~count- (unchecked-inc ~i-))
                           steppair-chunk (step recform-chunk (nnext exprs))
                           subform-chunk (steppair-chunk 1)]
                       [true
                        `(loop [~seq- (seq ~v), ~chunk- nil,
                                ~count- 0, ~i- 0]
                           (if (< ~i- ~count-)
                             (let [~k (.nth ~chunk- ~i-)]
                               ~subform-chunk
                               ~@(when needrec [recform-chunk]))
                             (when-let [~seq- (seq ~seq-)]
                               (if (chunked-seq? ~seq-)
                                 (let [c# (chunk-first ~seq-)]
                                   (recur (chunk-rest ~seq-) c#
                                          (int (count c#)) (int 0)))
                                 (let [~k (first ~seq-)]
                                   ~subform
                                   ~@(when needrec [recform]))))))])))))]
    (nth (step nil (seq seq-exprs)) 1)))
Vars in clojure.core/doseq:
Used in 0 other vars

Comments top

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