Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Clojure Introduction (Chinese)
Search
Yu Xin
June 12, 2013
Technology
0
67
Clojure Introduction (Chinese)
Introduce clojure language, very simple tutor about clojure.
Chinese
Yu Xin
June 12, 2013
Tweet
Share
More Decks by Yu Xin
See All by Yu Xin
Webpack
is
2
89
Other Decks in Technology
See All in Technology
なぜクラウドサービスで Web コンソールを提供するのか
shuta13
4
2k
CRTO/CRTL/OSEPの比較・勉強法とAV/EDRの検知実験
chayakonanaika
1
1.1k
開発生産性を始める前に開発チームができること / optim-improve-development-productivity.pdf
optim
0
100
アプリをリリースできる状態に保ったまま 段階的にリファクタリングするための 戦略と戦術 / Strategies and tactics for incremental refactoring
yanzm
6
1.3k
DroidKaigi 2024 たすけて!ViewModel
mhidaka
5
880
可視化により内部品質をあげるAIドキュメントリバース/20240910 Hiromitsu Akiba
shift_evolve
0
200
たった1人からはじめる【Agile Community of Practice】~ソース原理とFearless Changeを添えて~
ktc_corporate_it
1
430
o1のAPIで実験してみたが 制限きつすぎて辛かった話
pharma_x_tech
0
110
Oracle Autonomous Database:サービス概要のご紹介
oracle4engineer
PRO
1
7k
Swift Testingのconfirmationを コードリーディング/Dive into Swift Testing confirmation
laprasdrum
2
250
エンジニア視点で見る、 組織で運用されるデザインシステムにするには
shunya078
1
300
AIを活用した柔軟かつ効率的な社内リソース検索への取り組み
cygames
0
100
Featured
See All Featured
Testing 201, or: Great Expectations
jmmastey
36
7k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
96
Designing Dashboards & Data Visualisations in Web Apps
destraynor
227
52k
Build The Right Thing And Hit Your Dates
maggiecrowley
30
2.3k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
26
1.3k
Fantastic passwords and where to find them - at NoRuKo
philnash
48
2.8k
Designing the Hi-DPI Web
ddemaree
278
34k
KATA
mclloyd
27
13k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
663
120k
Music & Morning Musume
bryan
46
6k
Designing for Performance
lara
604
68k
Transcript
Clojure⼊入⻔门 技术六部 ⺩王东永 13年6月2⽇日星期⽇日
Clojure • Lisp on JVM • 动态语⾔言 • 函数式语⾔言 •
http://clojure.org (稳定版1.5.1,1.6 Alpha) 13年6月2⽇日星期⽇日
函数式编程 • 定义很多,仁者⻅见仁,智者⻅见智 • 泛函编程,是⼀一种编程范型,它将电脑 运算视为数学上的函数计算,并且避免 状态以及可变数据。 • 运算过程尽量写成⼀一系列嵌套的函数调 ⽤用
• 函数式编程是⼀一种编程模型,他将计算 机运算看做是数学中函数的计算,并且 避免了状态以及变量的概念 13年6月2⽇日星期⽇日
FP的特点 闭包和⾼高阶函数 函数是头等公民,参数,返回值 惰性计算 在需要产⽣生表达式的值时进⾏行计算 递归 控制流程的机制 引⽤用透明性 对函数的调⽤用可以被替换,却不会影响程序的⾏行为,惰性计算依赖该特 性
“⽆无”副作⽤用 FP 语⾔言不包含任何赋值语句,变量值⼀一旦被指派就永远不会改变 13年6月2⽇日星期⽇日
FP的优势 • 容易开发 • 容易阅读 • 容易测试 • 容易组合 •
易于并发 • ..... 13年6月2⽇日星期⽇日
cont. 数学表达式 过程式编程 函数式编程 13年6月2⽇日星期⽇日
cont. 13年6月2⽇日星期⽇日
Clojure:Not Only FP • 所有的Java类型/类库都可直接使⽤用 • 不可变性 (Immutability) • 性能优秀的Persistent
Data Structure • 做到Immutable的同时兼顾性能 • 强⼤大的Sequence⽀支持/Lazy Sequence • 宏 13年6月2⽇日星期⽇日
cont. • 对并发⽀支持良好 • Immutability本⾝身就对并发友好 • 可变状态是并发的天敌 • ⽀支持STM +
多种并发模型可⽤用 • Atom,Ref,Agent 多线程 • Var 线程本地 13年6月2⽇日星期⽇日
Clojure现状 • Open Source • Storm • Cascalog • ClojureScript
• leiningen • ... • 公司 • Amazon? • Twitter • Prismatic • 天涯 • 美味书签 13年6月2⽇日星期⽇日
Clojure at GitHub 13年6月2⽇日星期⽇日
Clojure at GitHub 13年6月2⽇日星期⽇日
Java程序员最应该学习 的FP 13年6月2⽇日星期⽇日
• 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⽇日星期⽇日
第⼀一个Clojure⼯工程 • $ lein new demo • $ cd demo
&& lein deps && lein pom • 将⼯工程导⼊入到IDEA 13年6月2⽇日星期⽇日
REPL • Read-Eval-Print Loop • 先编译,再执⾏行 • 交互式编程环境 • $
lein repl 13年6月2⽇日星期⽇日
REPL • Read-Eval-Print Loop • 先编译,再执⾏行 • 交互式编程环境 • $
lein repl 13年6月2⽇日星期⽇日
Hello World 13年6月2⽇日星期⽇日
Hello World 13年6月2⽇日星期⽇日
Hello World 13年6月2⽇日星期⽇日
Clojure语法 • Clojure语法: (operator operands*) • (println “hello”) • (+
1 2 3) • (def value 2) • (defn hello [x] (println “Hello,” x)) 13年6月2⽇日星期⽇日
(operator ...) • operator 可以是以下内容 • special form: def,let,fn,if •
宏:when,if-not,ns • 函数:println,+,-,*,/ • 前缀表达式 • (+ 1 2 3 4) (+ 1 (* 3 2)) 13年6月2⽇日星期⽇日
同像性 • Homoiconicity • 编程语⾔言的⼀一种属性,该语⾔言的基本表 现形式本⾝身也是该语⾔言⾃自⾝身的数据结 构 • Code is
data,data is code. • 求值等于解析数据结构 13年6月2⽇日星期⽇日
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⽇日星期⽇日
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⽇日星期⽇日
集合数据结构 • 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⽇日星期⽇日
Persistent Data Structure • Clojure内置的数据结构都是不可变的 • 每次更新会创建⼀一个新的数据结构 • 如果复制引起性能问题 •
解决⽅方案:共享数据,持久数据结构 13年6月2⽇日星期⽇日
cont. • (def a ‘(1 2)) => (1 2) •
(def b (cons 0 a)) => (0 1 2) 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
集合和Seq操作 13年6月2⽇日星期⽇日
forms • When a list is used to perform an
operation, it is called a form. • forms • 函数 • 宏 • special forms (built-in forms) 13年6月2⽇日星期⽇日
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⽇日星期⽇日
loop 和 recur • Clojure没有专⻔门的循环结构,通过递归实 现 • loop + recur
• loop 建⽴立递归点,recur跳转到递归点 13年6月2⽇日星期⽇日
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⽇日星期⽇日
宏 • 宏是Clojure元编程的主要⽅方式 • 扩展编译器,定义⾃自⼰己的语法结构 • DSL • 编译期⽣生效 13年6月2⽇日星期⽇日
cont. 13年6月2⽇日星期⽇日
cont. 13年6月2⽇日星期⽇日
函数 • 函数是⼀一等公民 • 参数 • 返回值 • 变量 •
defn定义函数 13年6月2⽇日星期⽇日
函数 • 函数是⼀一等公民 • 参数 • 返回值 • 变量 •
defn定义函数 13年6月2⽇日星期⽇日
⾼高阶函数 • 所谓⾼高阶函数,是指接受函数作为参数,或 者把函数作为返回值的函数 • Cojure提供了很多⾼高阶函数: • map,reduce,partial,comp,complement... 13年6月2⽇日星期⽇日
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⽇日星期⽇日
与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⽇日星期⽇日
cont. • (. Math PI) • Math/PI • (new java.util.Date)
• (java.util.Date.) • (. date getYear) • (.getYear date) 13年6月2⽇日星期⽇日
STM • Software Transactional Memory 13年6月2⽇日星期⽇日
⾯面向对象(Protocols) 13年6月2⽇日星期⽇日
More • multi-methods • defrecord / deftype • ns (命名空间)
• macro • ... 13年6月2⽇日星期⽇日
应⽤用例⼦子 • devops-alarm 项⺫⽬目介绍 • application/web/test • compojure/ring/hiccup/jetty • ClojureScript
• lein run / lein test/lein uberjar • java -jar 启动 13年6月2⽇日星期⽇日
参考资料 • http://clojure.org http://clojuredocs.org 13年6月2⽇日星期⽇日
Q & A 13年6月2⽇日星期⽇日