Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Clojure Introduction (Chinese)

Yu Xin
June 12, 2013

Clojure Introduction (Chinese)

Introduce clojure language, very simple tutor about clojure.
Chinese

Yu Xin

June 12, 2013

Transcript

55 slides
  1. Clojure • Lisp on JVM • 动态语⾔言 • 函数式语⾔言 •

    http://clojure.org (稳定版1.5.1,1.6 Alpha) 13年6月2⽇日星期⽇日
  2. Clojure:Not Only FP • 所有的Java类型/类库都可直接使⽤用 • 不可变性 (Immutability) • 性能优秀的Persistent

    Data Structure • 做到Immutable的同时兼顾性能 • 强⼤大的Sequence⽀支持/Lazy Sequence • 宏 13年6月2⽇日星期⽇日
  3. cont. • 对并发⽀支持良好 • Immutability本⾝身就对并发友好 • 可变状态是并发的天敌 • ⽀支持STM +

    多种并发模型可⽤用 • Atom,Ref,Agent 多线程 • Var 线程本地 13年6月2⽇日星期⽇日
  4. Clojure现状 • Open Source • Storm • Cascalog • ClojureScript

    • leiningen • ... • 公司 • Amazon? • Twitter • Prismatic • 天涯 • 美味书签 13年6月2⽇日星期⽇日
  5. • Leiningen is for automating Clojure projects without setting your

    hair on fire. • ⾯面向Clojure的构建⼯工具 • https://github.com/technomancy/leiningen • http://clojars.org/ Leiningen 13年6月2⽇日星期⽇日
  6. 第⼀一个Clojure⼯工程 • $ lein new demo • $ cd demo

    && lein deps && lein pom • 将⼯工程导⼊入到IDEA 13年6月2⽇日星期⽇日
  7. Clojure语法 • Clojure语法: (operator operands*) • (println “hello”) • (+

    1 2 3) • (def value 2) • (defn hello [x] (println “Hello,” x)) 13年6月2⽇日星期⽇日
  8. (operator ...) • operator 可以是以下内容 • special form: def,let,fn,if •

    宏:when,if-not,ns • 函数:println,+,-,*,/ • 前缀表达式 • (+ 1 2 3 4) (+ 1 (* 3 2)) 13年6月2⽇日星期⽇日
  9. Code VS. Data • (+ 1 2) => 3 •

    (def plus-3 (list ‘+ 1 2)) • (type plus-3) => clojure.lang.PersistentList • (eval plus-3) => 3 函数 参数 Code 数据 13年6月2⽇日星期⽇日
  10. Clojure的数据类型 • Integer 123 • Double 1.234 • BigDecimal 1.234m

    • Ratio 1/2 • String “Hello” • Character \a \b • Symbol ‘println • Keyword :key • Null nil • Boolean true false|nil • Regex #”123” 13年6月2⽇日星期⽇日
  11. 集合数据结构 • list 链表 • ‘(1 2 3) (list 1

    2 3) • vector 类似数组 • [1 2 3] • map key/value • {:id 1 :value 2} • set 集合 • #{1 2 3} • 都是⾮非可变的 • Persistent Data Structure 13年6月2⽇日星期⽇日
  12. cont. • (def a ‘(1 2)) => (1 2) •

    (def b (cons 0 a)) => (0 1 2) 13年6月2⽇日星期⽇日
  13. forms • When a list is used to perform an

    operation, it is called a form. • forms • 函数 • 宏 • special forms (built-in forms) 13年6月2⽇日星期⽇日
  14. special forms • (def symbol a) • 定义全局变量 • (do

    exprs*) • 顺序求值表达式 • (if test then else) • 条件语句 • (fn name? [params* ] exprs*) • 定义函数 • (let [bindings* ] exprs*) • 绑定值和变量 • var,loop,recur,try,thro w ... 13年6月2⽇日星期⽇日
  15. loop 和 recur • Clojure没有专⻔门的循环结构,通过递归实 现 • loop + recur

    • loop 建⽴立递归点,recur跳转到递归点 13年6月2⽇日星期⽇日
  16. loop 和 recur • Clojure没有专⻔门的循环结构,通过递归实 现 • loop + recur

    • loop 建⽴立递归点,recur跳转到递归点 1. recur in other than a tail position is an error 2. recur is the only non-stack-consuming looping construct in Clojure 3.recur is functional and its use in tail-position is verified by the compiler. 13年6月2⽇日星期⽇日
  17. cont. 问题 Java Clojure 求数组中 0的个数 int [] a =

    {0,2,3}; int count =0; for(int i=0;i<a.length;i++){ if(a[i] == 0){ count++; } } return count; (count (filter zero? [0 2 3 ])) 13年6月2⽇日星期⽇日
  18. 与Java互操作 • Clojure String = Java String • Clojure Number

    = Java Number • Clojure Collection实现 java.util.Collection (只 读) • Clojure函数实现了 Runnable和Callable接⼝口 • Clojure可以继承和实现 Java的类和接⼝口 • Clojure seq操作可以直 接⽤用于String,数组及 Iterable 13年6月2⽇日星期⽇日
  19. cont. • (. Math PI) • Math/PI • (new java.util.Date)

    • (java.util.Date.) • (. date getYear) • (.getYear date) 13年6月2⽇日星期⽇日
  20. More • multi-methods • defrecord / deftype • ns (命名空间)

    • macro • ... 13年6月2⽇日星期⽇日
  21. 应⽤用例⼦子 • devops-alarm 项⺫⽬目介绍 • application/web/test • compojure/ring/hiccup/jetty • ClojureScript

    • lein run / lein test/lein uberjar • java -jar 启动 13年6月2⽇日星期⽇日

More Decks by Yu Xin

Other Decks in Technology