Clojure 程式設計/示例/API 示例/do 宏
外觀
(doseq [i [1 2 3 4]] (print i))
; This is an example of using destructuring to pair an index with each element of a seq.
(doseq [[index word] (map vector
(iterate inc 0)
["one" "two" "three"])]
(prn (str index " " word)))
user=> (doall (map #(println "hi" %) ["mum" "dad" "sister"]))
hi mum hi dad hi sister (nil nil nil)
user=> (dorun (map #(println "hi" %) ["mum" "dad" "sister"]))
hi mum hi dad hi sister nil
(doto (new java.util.HashMap) (.put "a" 1) (.put "b" 2))
-> {a=1, b=2}
注意:doto 在修改後返回物件,非常方便。在上面的例子中,不需要變數繫結來訪問結果物件。
(.addChild *scene-graph*
(doto (KeyNavigatorBehavior.
(-> *universe* .getViewingPlatform .getViewPlatformTransform))
(setSchedulingBounds (BoundingSphere. (Point3d.) 10000.0))))
在這裡你可以看到使用 doto 比使用 let 建立臨時繫結更具可讀性。