doseq

added
1.0

ns
clojure.core

type
macro

(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.

                ;; Multiplies every x by every y.

(doseq [x [-1 0 1]
        y [1  2 3]] 
  (prn (* x y)))
-1
-2
-3
0
0
0
1
2
3
nil

;; Notice that the x iterates more slowly than y.

            
                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 (sorted-map :1 1 :2 2) (sorted-map :3 3 :4 4))]
         (prn (* b d)))
3
8
nil

;; where
user=> (map list (sorted-map :1 1 :2 2) (sorted-map :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
            
                ;; Keywords :let, :when, and :while are supported, the same as "for"
user=> (doseq [x (range 6)
               :when (odd? x)
               :let [y (* x x)] ]
         (println [x y]) )
[1 1]
[3 9]
[5 25]
nil

user=> (doseq [x (range 99)
               :let [y (* x x)] 
               :while (< y 30)
              ]
         (println [x y]) )
[0 0]
[1 1]
[2 4]
[3 9]
[4 16]
[5 25]
nil
user=> 

            
                ;; ClojureCLR example
;; Prints names of running processes

user=> (doseq [x (System.Diagnostics.Process/GetProcesses)] 
         (println (.get_ProcessName x)))
avgnt
SearchIndexer
svchost
chrome
audiodg
svchost
mbamscheduler
spoolsv
nvxdsync
avwebg7
GoogleCrashHandler64
svchost
CCleaner64
ViakaraokeSrv
...
            
                ;; simplest use

user=> (doseq [x [1 2 3 4 5]] (prn x))
1
2
3
4
5
nil
            
                ;;looping over a collection 

;;define CarMaker record
(defrecord CarMaker [cm-name])

;;define CarModel record
(defrecord CarModel [model-name cmaker doors color])

;;create a car-makes
(def car-maker {"cm1" (->CarMaker "Renault")})

;;createa models and add an CarMaker identifier to which model
(def models {
              "m1" (->CarModel "Megane" "cm1" 3 "Black")
              "m2" (->CarModel "Zoe" "cm1" 5 "White")
              })
;;println all model-name in the collection
(doseq [[k v] models] (println (:model-name (get models k))))
            
                ;;doseq  on vector
(def my-vector [1 2 3 "a" "b" "c" :a :b :c])
(doseq [v my-vector] (println v))

;;doseq on hash-map
(def my-map {:a "A" :b "B" :c "C" :d 1 :e 2 :f 3})
(doseq [[k v] my-map] (println k "->" v))