;;;; Snippet #1 user> (import '[java.util Random]) java.util.Random user> (def rnd (Random.)) #'user/rnd user> (defn kreisflaeche [radius] (* 2 Math/PI radius)) #'user/circle-area user> (let [radius (. rnd nextInt 20) flaech (kreisflaeche radius)] (println "radius:" radius "Flaeche:" flaech)) radius: 8 Flaeche: 50.26548245743669 nil ;;;; ;;;; Snippet #2 user> (import '[java.util TimeZone GregorianCalendar Locale]) java.util.Locale user> (Locale/US) # user> (doseq [id (filter #(re-find #"America/L" %) (TimeZone/getAvailableIDs))] (println id)) America/Los_Angeles America/Lima America/Louisville America/La_Paz nil user> (let [greg (GregorianCalendar. (TimeZone/getTimeZone "America/Los_Angeles") (Locale/US))] (.set greg 1993 11 4 6 0 0) (format "his final tour %tc" greg)) "his final tour Sa Dez 04 06:00:00 PST 1993" ;;;; ;;;; Snippet #3 user> (let [sb (StringBuilder.)] (.append sb "Hello") (.append sb " ") (.append sb "World") (.setCharAt sb 1 \a) (.replace sb 6 9 "Welt") (.deleteCharAt sb 11) (.deleteCharAt sb 10)) # ;;;; ;;;; Snippet #4 user> (doto (StringBuilder.) (.append "Hello") (.append " ") (.append "World") (.setCharAt 1 \a) (.replace 6 9 "Welt") (.deleteCharAt 11) (.deleteCharAt 10)) # ;;;; ;;;; Snippet #5 user> (import '[javax.xml.transform Transformer TransformerFactory]) javax.xml.transform.TransformerFactory user> (import '[javax.xml.transform.stream StreamSource StreamResult]) javax.xml.transform.stream.StreamResult user> (use '[clojure.contrib.java-utils :only (as-file)]) nil user> (.transform (.newTransformer (TransformerFactory/newInstance) (StreamSource. (as-file "listings/db.xsl"))) (StreamSource. (as-file "listings/source.xml")) (StreamResult. *out*)) Beispiel-XML Beschreibung Dieses Dokument dient als XML-Beispiel im Clojure-Buch. nil ;;;; ;;;; Snippet #6 user> (.. (TransformerFactory/newInstance) (newTransformer (StreamSource. (as-file "listings/db.xsl"))) (transform (StreamSource. (as-file "listings/source.xml")) (StreamResult. *out*))) ;; Ausgabe wie oben ;;;; ;;;; Snippet #7 (defmacro stream-file [file] `(StreamSource. (as-file ~file))) (.. (TransformerFactory/newInstance) (newTransformer (stream-file "listings/db.xsl")) (transform (stream-file "listings/source.xml") (StreamResult. *out*))) ;; Ausgabe wie oben ;;;; ;;;; Snippet #8 user> (use '(clojure.contrib [repl-utils :only (show)])) nil user> (import '[java.util Locale]) java.util.Locale user> (show Locale) ... [33] getCountry : String () ... [36] getDisplayLanguage : String () ... user> (select-keys (bean (Locale/getDefault)) [:country :displayLanguage]) {:displayLanguage "Deutsch", :country "DE"} ;;;; ;;;; Snippet #9 (defn t1 [anz] (let [a (make-array Integer/TYPE anz)] (dotimes [i anz] (aset a i i)))) (defn t2 [anz] (let [a (int-array anz)] (dotimes [i anz] (aset-int a i i)))) user> (time (t1 10000)) "Elapsed time: 560.231 msecs" nil user> (time (t2 10000)) "Elapsed time: 4.976 msecs" nil ;;;; ;;;; Snippet #10 (defn alle>1? [a] (areduce a i ret true (and (> (aget a i) 1) ret))) user> (alle>1? a1) false user> (alle>1? a2) true ;;;; ;;;; Snippet #11 user> (def a (into-array "hallo")) user> (aget (amap a i ret (Character/toUpperCase (aget a i))) 2) \L ;;;; ;;;; Snippet #12 user> (import [java.awt.event ActionListener] [javax.swing JFrame JButton]) javax.swing.JButton user> (doto (JFrame.) (.add (doto (JButton. "Picard") (.addActionListener (proxy [ActionListener] [] (actionPerformed [e] (println "Energie!")))))) (.pack) (.setVisible true)) # user> ;; Button druecken: Energie! ;;;; ;;;; Snippet #13 (ns de.clojure-buch.hildegard (:gen-class)) (defn -main [] (println "Bitte sagen Sie jetzt nichts, Hildegard")) ;;;; ;;;; Snippet #14 (ns de.clojure-buch.GenCl (:import [javax.swing JFrame JButton]) (:gen-class :implements [java.awt.event.ActionListener] :init init :constructors {[String] []} :state state)) ;;;; ;;;; Snippet #15 (defn -init [s] [[] (atom s)]) ;;;; ;;;; Snippet #16 (defn- -actionPerformed [this e] (println "Hello" @(.state this))) ;;;; ;;;; Snippet #17 shell> java -cp classes/:clojure.jar clojure.main Clojure 1.2.0 user=> (import [de.clojure_buch GenCl]) de.clojure_buch.GenCl user=> (import [javax.swing JFrame JButton]) javax.swing.JButton user=> (doto (JFrame.) (.add (doto (JButton. "Hello") (.addActionListener (GenCl. "Clojure")))) (.pack) (.setVisible true)) # step (+ spring nimm)) acc (if (< step spring) (recur (logabb x) (inc step) acc) (recur (logabb x) (inc step) (conj acc x))))))) ;;;; ;;;; Snippet #20 (defn starte-berechnung [] (dorun (pmap (fn [mu] (let [xs (logistische-iteriert mu 5000 100) points (vec (map (fn [x] [mu x]) xs))] (swap! main-data into points))) (range 2.90 4 0.001)))) ;;;; ;;;; Snippet #21 (defn minmax [acc neu] {:xmin (min (:xmin acc) (nth neu 0)) :xmax (max (:xmax acc) (nth neu 0)) :ymin (min (:ymin acc) (nth neu 1)) :ymax (max (:ymax acc) (nth neu 1))}) ;;;; ;;;; Snippet #22 bifurkation> (def test-daten [[0 1] [1 2] [0 0]]) bifurkation> (let [[x1 y1] (first test-daten)] (reduce minmax {:xmin x1 :xmax x1 :ymin y1 :ymax y1} test-daten)) {:xmin 0, :xmax 1, :ymin 0, :ymax 2} ;;;; ;;;; Snippet #23 (defn normalize [data sx sy] (let [[x1 y1] (first data) minmax (reduce minmax {:xmin x1 :xmax x1 :ymin y1 :ymax y1} data) xmin (float (:xmin minmax)) ymin (float (:ymin minmax)) xmax (float (:xmax minmax)) ymax (float (:ymax minmax))] (map (fn [[x y]] [(int (* sx (/ (- x xmin) (- xmax xmin)))) (int (- sy (* sy (/ (- y ymin) (- ymax ymin)))))]) data))) ;;;; ;;;; Snippet #24 (defn paint-data-img-dots [g sx sy] (let [data @main-data] (if (< 0 (count data)) (let [img (BufferedImage. (inc sx) (inc sy) BufferedImage/TYPE_INT_ARGB) d (normalize data sx sy) rgb (.getRGB (Color/black))] (doseq [[x y] d] (.setRGB img x y rgb)) (.drawImage g img 0 0 nil)) (println "Warte auf Daten ...")))) ;;;; ;;;; Snippet #25 (defn swing-setup [timer sizex sizey] (let [p (doto (proxy [JPanel] [] (paintComponent [g] (println "Repaint") (proxy-super paintComponent g) (paint-data-img-dots g sizex sizey))) (.setBorder (BorderFactory/createLineBorder (Color/black))) (.setPreferredSize (Dimension. sizex sizey))) w (doto (JFrame. "CLJ Bifurkation") (.add p) (.pack) (.setVisible true))] (.addActionListener timer (proxy [java.awt.event.ActionListener] [] (actionPerformed [e] (println "Timer rennt") (.repaint w))))) (.start timer)) ;;;; ;;;; Snippet #26 (defn bifurkation [] (let [timer (Timer. 250 nil)] (SwingUtilities/invokeLater #(swing-setup timer 1000 500)) (dosync (reset! main-data [])) (starte-berechnung) (Thread/sleep 5000) (.stop timer) timer)) ;;;; ;;;; Snippet #27 (defn std-abw [xs] (let [n (count xs) mw (/ (reduce + xs) n) quads (map #(Math/pow (- % mw) 2) xs)] (Math/sqrt (/ (reduce + quads) (dec n))))) ;;;; ;;;; Snippet #28 (defn viele-berechnungen [wie-oft] (dotimes [_ wie-oft] (time (std-abw (vec (take 10000 (repeatedly #(rand-int 100)))))))) user> (viele-berechnungen 20) "Elapsed time: 1155.321966 msecs" "Elapsed time: 362.805691 msecs" "Elapsed time: 472.227636 msecs" "Elapsed time: 358.231114 msecs" "Elapsed time: 170.326634 msecs" "Elapsed time: 120.865899 msecs" "Elapsed time: 118.956424 msecs" "Elapsed time: 118.087103 msecs" "Elapsed time: 120.922323 msecs" "Elapsed time: 118.793614 msecs" "Elapsed time: 111.64418 msecs" "Elapsed time: 113.938075 msecs" "Elapsed time: 111.807744 msecs" "Elapsed time: 110.866336 msecs" "Elapsed time: 117.818978 msecs" "Elapsed time: 115.045278 msecs" "Elapsed time: 112.953086 msecs" "Elapsed time: 114.27616 msecs" "Elapsed time: 124.001107 msecs" "Elapsed time: 108.299311 msecs" nil ;;;; ;;;; Snippet #29 (defn std-abw [xs] (let [n (count xs) mw (/ (reduce + xs) n) quads (pmap #(Math/pow (- % mw) 2) xs)] (Math/sqrt (/ (reduce + quads) (dec n))))) user> (viele-berechnungen 90) "Elapsed time: 921.151366 msecs" "Elapsed time: 517.00259 msecs" ;; ... "Elapsed time: 368.08286 msecs" "Elapsed time: 380.962187 msecs" nil ;;;; ;;;; Snippet #30 (defn std-abw [xs] (let [n (count xs) mw (float (/ (reduce + xs) n)) quads (map #(Math/pow (- % mw) 2) xs)] (Math/sqrt (/ (reduce + quads) (dec n))))) user> (viele-berechnungen 4) "Elapsed time: 28.747785 msecs" "Elapsed time: 14.277022 msecs" "Elapsed time: 8.10453 msecs" "Elapsed time: 8.002072 msecs" ;;;;