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
79
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
100
Other Decks in Technology
See All in Technology
Definition of Done
kawaguti
PRO
6
450
知識を整理して未来を作る 〜SKDとAI協業への助走〜
yosh1995
0
120
讓測試不再 BB! 從 BDD 到 CI/CD, 不靠人力也能 MVP
line_developers_tw
PRO
0
1.1k
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
53
31k
AWS アーキテクチャ作図入門/aws-architecture-diagram-101
ma2shita
29
9.5k
本部長の代わりに提案書レビュー! KDDI営業が毎日使うAIエージェント「A-BOSS」開発秘話
minorun365
PRO
14
2.3k
CI/CDとタスク共有で加速するVibe Coding
tnbe21
0
230
BigQuery Remote FunctionでLooker Studioをインタラクティブ化
cuebic9bic
2
210
Snowflake Summit 2025全体振り返り / Snowflake Summit 2025 Overall Review
mtpooh
2
180
ローカルLLMでファインチューニング
knishioka
0
120
キャディでのApache Iceberg, Trino採用事例 -Apache Iceberg and Trino Usecase in CADDi--
caddi_eng
0
170
AIのAIによるAIのための出力評価と改善
chocoyama
0
440
Featured
See All Featured
Become a Pro
speakerdeck
PRO
28
5.4k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Being A Developer After 40
akosma
90
590k
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.3k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
4 Signs Your Business is Dying
shpigford
184
22k
Building an army of robots
kneath
306
45k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
48
2.8k
Writing Fast Ruby
sferik
628
61k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
The Straight Up "How To Draw Better" Workshop
denniskardys
233
140k
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⽇日星期⽇日