Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
84
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
120
Other Decks in Technology
See All in Technology
手動から自動へ、そしてその先へ
moritamasami
0
260
たかが特別な時間の終わり / It's Only the End of Special Time
watany
28
7.6k
Uncertainty in the LLM era - Science, more than scale
gaelvaroquaux
0
700
21st ACRi Webinar - Univ of Tokyo Presentation Slide (Ayumi Ohno)
nao_sumikawa
0
120
生成AIでテスト設計はどこまでできる? 「テスト粒度」を操るテーラリング術
shota_kusaba
0
290
“決まらない”NSM設計への処方箋 〜ビットキーにおける現実的な指標デザイン事例〜 / A Prescription for "Stuck" NSM Design: Bitkey’s Practical Case Study
bitkey
PRO
1
510
世界最速級 memcached 互換サーバー作った
yasukata
0
270
エンジニアリングマネージャー はじめての目標設定と評価
halkt
0
210
Multimodal AI Driving Solutions to Societal Challenges
keio_smilab
PRO
1
130
Security Diaries of an Open Source IAM
ahus1
0
130
HIG学習用スライド
yuukiw00w
0
110
Claude Code はじめてガイド -1時間で学べるAI駆動開発の基本と実践-
oikon48
45
27k
Featured
See All Featured
Speed Design
sergeychernyshev
33
1.4k
Code Review Best Practice
trishagee
73
19k
[RailsConf 2023] Rails as a piece of cake
palkan
58
6.1k
Embracing the Ebb and Flow
colly
88
4.9k
Writing Fast Ruby
sferik
630
62k
Product Roadmaps are Hard
iamctodd
PRO
55
12k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
253
22k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
Optimising Largest Contentful Paint
csswizardry
37
3.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
The Pragmatic Product Professional
lauravandoore
37
7.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
46
2.6k
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⽇日星期⽇日