Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Clojure入門

13,163 views

Published on

Introduction to Clojure Programming.

Published in: Technology
  • Be the first to comment

Clojure入門

  1. 1. (defun hello (name) (print (concatenate 'string "Hello, " name "!"))) (hello "Common Lisp") (define (hello name) (print "Hello, " name "!")) (hello "Scheme") (defn hello [name] (print (str "Hello, " name "!"))) (hello "Clojure")
  2. 2. def hello(name) puts "Hello, " + name + "!" end hello "Ruby" (defn hello [name] (print (str "Hello, " name "!"))) (hello "Clojure")
  3. 3. def hello(name) puts "Hello, " + name + "!" end hello "Ruby" (defn hello [name] (print (str "Hello, " name "!"))) (hello "Clojure")
  4. 4. (1 2 3) ["apple" "google" "microsoft"] {:artist "Michael Jackson" :song "Thriller"}
  5. 5. person.getAddress().getZipCode() (.getZipCode (.getAddress person))
  6. 6. person.getAddress().getZipCode() (.getZipCode (.getAddress person))
  7. 7. person.getAddress().getZipCode() (.getZipCode (.getAddress person))
  8. 8. person.getAddress().getZipCode() (.getZipCode (.getAddress person))
  9. 9. person.getAddress().getZipCode() (.getZipCode (.getAddress person))
  10. 10. person.getAddress().getZipCode() (.. person getAddress getZipCode)
  11. 11. (car (list 1 3 5 7 9)) ; -> 1 (cdr (list 1 3 5 7 9)) ; -> (3 5 7 9)
  12. 12. (first (list 1 3 5 7 9)) ; -> 1 (second (list 1 3 5 7 9)) ; -> 3 (rest (list 1 3 5 7 9)) ; -> (3 5 7 9)
  13. 13. #!/bin/sh java -cp /path/to/clojure.jar clojure.main $* $ clj Clojure 1.2.0-master-SNAPSHOT user=> $ clj hello.clj Hello, Clojure!
  14. 14. $ curl -O http://github.com/technomancy/leiningen/raw/stable/bin/lein $ mv lein ~/bin/ $ chmod +x ~/bin/lein $ lein self-install $ lein repl Clojure 1.1.0-master-SNAPSHOT user=>
  15. 15. user=> (doc first) (doc first) ------------------------- clojure.core/first ([coll]) Returns the first item in the collection. Calls seq on its argument. If coll is nil, returns nil.
  16. 16. user=> (use 'clojure.contrib.repl-utils) (use 'clojure.contrib.repl-utils) nil user=> (source +) (source +) (defn + "Returns the sum of nums. (+) returns 0." {:inline (fn [x y] `(. clojure.lang.Numbers (add ~x ~y))) :inline-arities #{2}} ...
  17. 17. user=> (use 'clojure.contrib.repl-utils) (use 'clojure.contrib.repl-utils) nil user=> (show java.net.InetAddress) (show java.net.InetAddress) === public java.net.InetAddress === [ 0] static getAllByName : InetAddress[] (String) [ 1] static getByAddress : InetAddress (String,byte[]) [ 2] static getByAddress : InetAddress (byte[]) ...
  18. 18. $ cat abc.txt abc defg Clojure
  19. 19. user=> (slurp "abc.txt") "abcndefgnnClojure n" user=> (print (slurp "abc.txt")) abc defg Clojure nil user=>
  20. 20. user=> (use '[clojure.contrib.duck-streams :only (read-lines)]) nil user=> (doseq [line (read-lines "abc.txt")] (println line)) abc defg Clojure nil
  21. 21. for (String line : lines) { System.out.println(line); } (doseq [line lines] (println line))
  22. 22. user=> (use '[clojure.contrib.duck-streams :only (reader)]) nil user=> (doseq [line (line-seq (reader (java.io.File. "abc.txt")))] (println line)) abc defg Clojure nil
  23. 23. user=> (with-open [rdr (java.io.BufferedReader. (java.io.FileReader. "abc.txt"))] (doseq [line (line-seq rdr)] (println line))) abc defg Clojure nil

×