Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Clojure Introduction (Chinese)
Search
Yu Xin
June 12, 2013
Technology
120
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
メルペイ 会計システム概要と歴史
mewuto
0
190
いちAWSエンジニアのAI活用を振り返る #devio2026 / devio osaka 2026 kawahara
masahirokawahara
2
390
「Docker Desktop不要の時代」は 来たのか?
ueponx
0
110
Snowflake Horizon Catalog と Apache Iceberg で作る オープンなデータ基盤
kitagawaz
0
450
私の推しは「聞いてから進む」AIです -AI-DLCに一人でアプリを作らせた話
yama3133
1
180
子育てエンジニアの可処分時間が減っても成長を諦めない戦い方
sudoakiy
1
1.4k
自律型 AI をセキュアに実装!Gemini と MIG で作る動的コード実行環境
recruitengineers
PRO
1
220
ログラスのマルチプロダクトを 支える認証基盤 〜テナントごとに異なる統制とどう向き合うか〜
dada4386
3
400
形式手法を使って仕様をコーディングしよう
mikanichinose
0
180
AgentCoreで実践するハーネスエンジニアリング
yakumo
1
290
Datadog の学び方 - あるいは、オブザーバビリティを学ぶとは何か
mananyuki
1
490
Deep Data Security 機能解説
oracle4engineer
PRO
2
710
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
20k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
390
Getting science done with accelerated Python computing platforms
jacobtomlinson
2
520
Believing is Seeing
oripsolob
1
240
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
2
6.3k
What does AI have to do with Human Rights?
axbom
PRO
1
2.4k
Facilitating Awesome Meetings
lara
57
7.2k
My Coaching Mixtape
mlcsv
0
340
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
234
18k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
300
Chasing Engaging Ingredients in Design
codingconduct
0
340
WCS-LA-2024
lcolladotor
0
850
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⽇日星期⽇日