;;;; Snippet #1
(use '[clojure.java.io :only
(input-stream output-stream copy)])
(defn wget [input output]
(with-open [is (input-stream input)]
(with-open [os (output-stream output)]
(copy is os))))
;; Verwendung:
user> (wget "http://example.com/test.zip"
"test.zip")
nil
;; oder auch
user> (wget "/tmp/quelle" "/tmp/ziel")
nil
;;;;
;;;; Snippet #2
(def die-url
(str "http://github.com/richhickey/clojure/raw/"
"9694a92d84ddffb6794fa97efd429b5e23285553/"
"pom-template.xml"))
user> (clojure.xml/parse die-url)
{:tag :project,
:attrs {:xmlns "http://maven.apache.org/POM/4.0.0",
;; Ausgabe gekuerzt...
},
:content
[{:tag :modelVersion,
:attrs nil,
:content ["4.0.0"]}
;; Ausgabe gekuerzt
{:tag :artifactId, :attrs nil, :content ["clojure"]}
;; Ausgabe gekuerzt
]}
;;;;
;;;; Snippet #3
user> (use '(clojure [pprint :only (pprint)]))
nil
user> (use 'clojure.contrib.lazy-xml)
nil
user> (def xml-data
"hello")
#'user/xml-data
user> (pprint (parse-seq
(java.io.ByteArrayInputStream.
(.getBytes xml-data))))
({:type :start-element, :name :root, :attrs {}, :str nil}
{:type :start-element, :name :foo, :attrs {}, :str nil}
{:type :start-element, :name :bar, :attrs {}, :str nil}
{:type :characters, :name nil, :attrs nil, :str "hello"}
{:type :end-element, :name :bar, :attrs nil, :str nil}
{:type :end-element, :name :foo, :attrs nil, :str nil}
{:type :start-element, :name :baz, :attrs {}, :str nil}
{:type :end-element, :name :baz, :attrs nil, :str nil}
{:type :end-element, :name :root, :attrs nil, :str nil})
nil
;;;;
;;;; Snippet #4
user> (use 'de.clojure-buch.lazy-reddit-rss)
nil
user> (doseq [e (parse-rss
"http://www.reddit.com/r/clojure.rss")]
(printf "%s\n%s\n\n" (first e) (fnext e)))
;; Ausgabe entfernt
;;;;
;;;; Snippet #5
user> (use 'de.clojure-buch.lsystem)
nil
user> (use 'clojure.test)
nil
user> (run-tests 'de.clojure-buch.lsystem)
Testing de.clojure-buch.lsystem
FAIL in (cantor-lsystem-failing) (lsystem.clj:53)
expected:
;; Ausgabe gekuerzt...
actual: (not (clojure.core/= (:A :B :A :X) (:A :B :A)))
Ran 2 tests containing 12 assertions.
1 failures, 0 errors.
{:type :summary, :test 2, :pass 11, :fail 1, :error 0}
;;;;
;;;; Snippet #6
user> (use 'de.clojure-buch.api-search)
nil
user> (use 'clojure.repl)
nil
user> (find-usage-in-ns 'filter)
(find-protocol-impl filter ns remove flatten)
user> (source remove)
(defn remove
;; Ausgabe gekürzt
[pred coll]
(filter (complement pred) coll))
nil
;;;;
;;;; Snippet #7
(defn ping [arg]
(str "ping: " (apply str (reverse arg))))
(defn pong [arg]
(str "pong: " (ping (apply str (reverse arg)))))
user> (use 'clojure.contrib.trace)
nil
user> (dotrace [ping pong]
(pong "O Genie, der Herr ehre Dein Ego"))
TRACE t1969: (pong "O Genie, der Herr ehre Dein Ego")
TRACE t1970: | (ping "ogE nieD erhe rreH red ,eineG O")
TRACE t1970: | => "ping: O Genie, der Herr ehre Dein Ego"
TRACE t1969: => "pong: ping: O Genie, der Herr ehre Dein Ego"
"pong: ping: O Genie, der Herr ehre Dein Ego"
user> (dotrace [ping]
(pong "O Genie, der Herr ehre Dein Ego"))
TRACE t2852: (ping "ogE nieD erhe rreH red ,eineG O")
TRACE t2852: => "ping: O Genie, der Herr ehre Dein Ego"
"pong: ping: O Genie, der Herr ehre Dein Ego"
;;;;
;;;; Snippet #8
CREATE TABLE IF NOT EXISTS `FINDEX` (
`id` bigint unsigned NOT NULL auto_increment,
`fpath` varchar(4096) NOT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
;;;;
;;;; Snippet #9
(def conn-parm
{:user "jimknopf"
:pass "molly"
:host "localhost"
:port "3306"
:db "cljtest"
})
(def db {:classname "com.mysql.jdbc.Driver"
:subprotocol "mysql"
:subname (str "//" (conn-parm :host)
":" (conn-parm :port)
"/" (conn-parm :db))
:user (conn-parm :user)
:password (conn-parm :pass)})
(use 'clojure.contrib.sql)
;;;;
;;;; Snippet #10
;; index files
user> (with-connection db
(apply
insert-records
:FINDEX
(map
(fn [f] {:fpath (.getPath f)})
(file-seq (java.io.File. "/etc/java-6-sun/")))))
;;;;
;;;; Snippet #11
(defn files-in-db []
(let [sql "select FPATH from FINDEX"]
(with-query-results rows [sql]
(into [] rows))))
user> (with-connection db
(dorun
(map println
(files-in-db))))
{:fpath /etc/java-6-sun}
{:fpath /etc/java-6-sun/content-types.properties}
;; Ausgabe verkürzt
{:fpath /etc/java-6-sun/net.properties}
{:fpath /etc/java-6-sun/calendars.properties}
nil
;;;;
;;;; Snippet #12
;; erzeuge zwei fehler
user> (with-connection db
(insert-records
:FINDEX
{:fpath "GibtEsNicht"}
{:fpath "gibt.es.auch.nicht"}))
nil
;;;;
;;;; Snippet #13
(defn file-exists? [sysf]
"pruefe files auf vorhandensein."
(.exists (java.io.File. sysf)))
user> (with-connection db
(filter (complement #(file-exists? (:fpath %)))
(files-in-db)))
({:fpath "GibtEsNicht"}
{:fpath "gibt.es.auch.nicht"})
;;;;
;;;; Snippet #14
user> (use 'clojure.contrib.dataflow)
nil
(defn not-negative? [a]
(if (< a 0)
(throw (RuntimeException. "Value is not positive"))))
(defn print-flow-values [df key-seq]
(apply concat
(map #(vector (keyword %) (get-value df %))
key-seq)))
(def rectangle-geometry-flow
(build-dataflow
[(cell :source width 0)
(cell :source height 0)
(cell area (* ?height ?width))
(cell circumference (+ (* 2 ?height) (* 2 ?width)))
(cell :validator (not-negative? ?height))
(cell :validator (not-negative? ?width))
]))
user> (update-values rectangle-geometry-flow
{'width 5 'height 10})
nil
user> (print-flow-values rectangle-geometry-flow
'(height width area circumference))
(:height 10 :width 5 :area 50 :circumference 30)
user> (try
(update-values rectangle-geometry-flow {'width -5})
(catch Throwable e (println e)))
#
nil
;;;;
;;;; Snippet #15
(def price-calc
(build-dataflow
[(cell :source item-price 10)
(cell :source item-count 5)
(cell discount (if (> ?item-count 3) 0.95 1))
(cell discount (if (> ?item-price 5) 0.8 1))
(cell total-price (* ?item-price ?item-count))
(cell final-price (* ?total-price
(reduce * ?*discount)))]))
;;;;