2012-04-14

Clojure

大部份資料都 Google 得到,這裡只是做個筆記。 下載 1.3.0 版後,直接解開來就可以用了。當然如果安裝 lein 會更優。lein 會把所有的東西都放在自己的目錄裡,所以在這家公司也沒問題。
C:\download\clojure-1.3.0>java -jar clojure-1.3.0.jar
Clojure 1.3.0
user=> (println "Hello World")
Hello World
nil
user=> (javax.swing.JOptionPane/showMessageDialog nil "Hello World")
nil
user=>
資料: 筆記:
; System.out.println()
(println "Hello World")
; Swing
(javax.swing.JOptionPane/showMessageDialog nil "Hello World")
;
; Name space
;
(def v1 10)     ; user/v1
(in-ns 'myapp)
(def v1 100)    ; myapp/v1
(in-ns 'user)
v1              ; = 10
myapp/v1        ; = 100
;
; Polymorphism
;
(defn force-it
    "The first function a young jedi needs"
    ([]
        (str "Use the force, Luke"))
    ([jedi]
        (str "Use the force, " jedi)))
;
; Lambda
;
(map (fn [x] (* 2 x)) v)  ; v = (2 4 6 8)
(map #(* 2 %) v)          ; #() = lambda
;
;
;
;
;
;
(take 10 (cycle [1 2 3 4]))
; ==> (1 2 3 4 1 2 3 4 1 2)
(interleave [:a :b :c :d :e] [1 2 3 4 5])
; ==> (:a 1 :b 2 :c 3 :d 4 :e 5)
(partition 3 [1 2 3 4 5 6 7 8 9 10])
; ==> ((1 2 3) (4 5 6) (7 8 9))
(map vector [:a :b :c :d :e] [1 2 3 4 5])
; ==> ([:a 1] [:b 2] [:c 3] [:d 4] [:e 5])
(apply str (interpose \, "asdf"))
; ==> "a,s,d,f"
(interpose \, "asdf")
; ==> (\a \, \s \, \d \, \f)
(System/getProperties)
; ==> #<Properties {java.runtime.name=Diablo Java(TM) SE Runtime Environment, sun.boot.library.path=/usr/local/diablo-jdk1.6.0/jre/lib/amd64, ...
(first (System/getProperties))
; ==> #<Entry java.runtime.name=Diablo Java(TM) SE Runtime Environment>
(.getBytes "Hello")
; ==> #<byte[] [B@5eea3cdf>
(first (.getBytes "Hello"))
; ==> 72
(re-seq #"\w+" "the quick brown fox")
; ==> ("the" "quick" "brown" "fox")
(sort (re-seq #"\w+" "the quick brown fox"))
; ==> ("brown" "fox" "quick" "the")
(java.io.File. ".")
; ==> #<File .>
(take 5 (file-seq (java.io.File. ".")))
; ==> (#<File .> #<File ./.cshrc> #<File ./.login> #<File ./.mail_aliases> #<File ./.login_conf>)
(count (file-seq (java.io.File. ".")))
; ==> 16760
(with-open [rdr (reader "README")] (count (line-seq rdr)))
; ==> CompilerException java.lang.RuntimeException: Unable to resolve symbol: reader in this context, compiling:(NO_SOURCE_PATH:5)
(use '[clojure.contrib.repl-utils :only (show)])
; ==< nil
(show java.util.Collections "sort")
; ==< ===  public java.util.Collections  ===
; ==< [11] static checkedSortedMap : SortedMap (SortedMap,Class,Class)
; ==< [12] static checkedSortedSet : SortedSet (SortedSet,Class)
; ==< [40] static sort : void (List)
; ==< [41] static sort : void (List,Comparator)
; ==< [47] static synchronizedSortedMap : SortedMap (SortedMap)
; ==< [48] static synchronizedSortedSet : SortedSet (SortedSet)
; ==< [53] static unmodifiableSortedMap : SortedMap (SortedMap)
; ==< [54] static unmodifiableSortedSet : SortedSet (SortedSet)
(slurp "readme.txt")
; ==< 讀出 readme.txt 的內容
(use '[clojure.contrib.duck-streams :only (read-lines)])
; ==< nil
(doseq [line (read-lines "readme.txt")] (println line))
; ==< 印出 readme.txt 的內容
(java.io.File. "readme.txt")
; ==< #>File readme.txt<
(reader (java.io.File. "readme.txt"))
; ==< #>BufferedReader java.io.BufferedReader@42db681c<
(line-seq (reader (java.io.File. "readme.txt")))
; ==< 印出 readme.txt 的內容
(with-open [rdr (java.io.BufferedReader. (java.io.FileReader. "readme.txt"))] (doseq [line (line-seq rdr)] (println line)))
; ==< 印出 readme.txt 的內容
(doc line-seq)
; ==< java.io.BufferedReader 的 lazy-seq!
(def book {:title "1Q84" :author "村上春樹"})
; ==< #'user/book
(assoc book :publisher "新潮社")
; ==< {:publisher "新潮社", :title "1Q84", :author "村上春樹"}
book
; ==< {:title "1Q84", :author "村上春樹"}

沒有留言: