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
110
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Clojure Introduction (Chinese)
Introduce clojure language, very simple tutor about clojure.
Chinese
Yu Xin
June 12, 2013
More Decks by Yu Xin
See All by Yu Xin
Webpack
is
2
150
Other Decks in Technology
See All in Technology
誤解だらけの開発生産性 / Myths and Misconceptions about Developer Productivity
i35_267
1
240
ローカルLLMとLINE Botの組み合わせ その3 / LINE DC Generative AI Meetup #8
you
PRO
0
130
CIで使うClaude
iwatatomoya
0
240
DMM.com 購入改善推進チーム におけるCodeRabbitを用いた レビューフロー改善の一例
ysknsid25
2
620
証券システムを10年Scalaで作り続けるということ - 関数型まつり2026
krrrr38
3
840
ボーイスカウトルールでメモリやスキルを改善しよう
azukiazusa1
1
740
ループエンジニアリングでE2Eテストを実践
noriyukitakei
0
350
はじめてのWDM
miyukichi_ospf
1
140
Claude Code公式skillで 自分の仕事を少しずつ手放そう!(Claude Code開発ノウハウ大公開スペシャル by クラスメソッド)
kaym
1
280
脱金融のフューチャー・デザイン / Future Design Beyond Finance
ks91
PRO
0
150
あなたの『Site』はどこですか? — xREという考え方
miyamu
0
1.2k
Making sense of Google’s agentic dev tools
glaforge
1
190
Featured
See All Featured
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
65
56k
VelocityConf: Rendering Performance Case Studies
addyosmani
333
25k
Believing is Seeing
oripsolob
1
170
Navigating Weather and Climate Data
rabernat
0
310
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
2.1k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
230
23k
How STYLIGHT went responsive
nonsquared
100
6.2k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.9k
The Language of Interfaces
destraynor
162
27k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
3
920
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.2k
Docker and Python
trallard
47
3.9k
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⽇日星期⽇日