ClojureDocs

导航

名称空间

doseq

clojure.core

从 1.0 开始提供 (源代码)
  • (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.
15 Examples
;; Multiplies every x by every y.

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

;; Notice that y is iterated over for every x. x could be seen as the 'outer' loop
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))
user=> (doseq  [y (range 10)] 
         (dotimes [z y] (print (inc z)))
         (newline))

1
12
123
1234
12345
123456
1234567
12345678
123456789
nil
;; nothing will print
(doseq [x []
        y [1 2 3]]
    (prn x y))

;; if any bindings have empty sequences, the body will never execute
;; to iterate across a map
(doseq [[k v] {:a 1 :b 10 :c 100}]
  (println (list k v)))

;; outputs
(:a 1)
(:b 10)
(:c 100)
nil
;; Iterate through a simple list
(doseq  [number [1 2 3 4]] 
    (println number))
;; Output
1
2
3
4
nil
;; The `:while` modifier triggers the inner-most loop to be aborted.
;; (to break out of the inner most loop).

(doseq [a (range 5)
        b (range 5)
        :while (even? b) ;; when be fails to be even, then break out of b loop
       ]
  (println {:a a :b b})
;; Output
{:a 0, :b 0}
{:a 1, :b 0}
{:a 2, :b 0}
{:a 3, :b 0}
{:a 4, :b 0}
See Also

When lazy sequences are produced via functions that have side effects, any effects other than thos...

Added by boxie

When lazy sequences are produced via functions that have side effects, any effects other than thos...

Added by boxie

List comprehension. Takes a vector of one or more binding-form/collection-expr pairs, each follow...

Added by mmwaikar

bindings => name n Repeatedly executes body (presumably for side-effects) with name bound to in...

Runs the supplied procedure (via reduce), for purposes of side effects, on successive items in the...

Added by mars0i
0 Notes
No notes for doseq