第三章 函数式编程
函数式编程(Functional Programming)就像是一辆高端,新潮的氢燃料汽车,虽然还未被广泛应用,但是二十年之后我们的生活将与它密不可分。
函数式编程与命令式编程不同。命令式编程的代码由一系列改变全局状态的语句构成,而函数式编程则是将计算过程抽象成表达式求值。函数式编程可以很容易做到线程安全,因此特别适合并发编程。
3.1 若不爽,就另辟蹊径
函数式编程没有可变状态,所以不会遇到由共享可变状态带来的种种问题。本章使用clojure介绍函数式编程。
3.2 第一天:抛弃可变状态
可变状态的风险
- 隐藏的可变状态 SimpleDateFormat
- 逃逸的可变状态
Clojure旋风之旅
user=> (max 3 5)
5
user=> (+ 1 (* 2 3))
7
user=> (def meaning-of-life 42)
#'user/meaning-of-life
user=> meaning-of-life
42
user=> (if(< meaning-of-life 0) "negative" "non-negative")
"non-negative"
user=> (def droids["huey""Dewey""Louie"])
#'user/droids
user=> (count droids)
3
user=> (droids 0)
"huey"
user=> (def me {:name "Paul" :age 45 :sex :male})
#'user/me
user=> (:age me)
45
user=> (:sex me)
:male
user=>
第一个函数式程序
我们一直说函数式编程最有趣的地方是不使用可变状态,下面来说个例子。先看java的:
public int sum(int[] numbers){
int accumulator = 0;
for(int n : numbers){
accumulator += n;
}
return accumulator;
}
代码中accumulator是可变的,因此这段代码不是函数式的。下面使用clojure的方案:
//方式一
(defn recursive-sum [numbers]
(if (empty? numbers)
0
(+ (first numbers) (recursive-sum (rest numbers)))
)
)
user=> (recursive-sum [1,2,3])
6
方式二
(defn reduce-sum [numbers]
(reduce (fn [acc x] (+ acc x)) 0 numbers)
)
user=> (reduce-sum [1,2,3])
6
方式三:
(defn sum [numbers]
(reduce + numbers)
)
user=> (sum [1,2,3])
6
方式三中+
是个函数例如:
(+ 1 2 3 4)
顺便一提,由于+
可以接受若干参数,因此我们也可以用apply来实现sum函数。apply可以接受一个函数和一个矢量,调用函数时将这个矢量展开作为函数的参数:
(defn apply-sum [numbers]
(apply + numbers)
)
轻松并行
将上面函数式代码改成并行
(ns sum.core
(:require [clojure.core.reducers :as r]))
(defn parallel-sum [numbers]
(r/fold + numbers)
)
对比下性能提升:
// 串行
sum.core=> (time (sum numbers))
"Elapsed time: 446.409094 msecs"
49999995000000
sum.core=> (time (sum numbers))
"Elapsed time: 2681.880657 msecs"
49999995000000
sum.core=> (time (sum numbers))
"Elapsed time: 148.962636 msecs"
49999995000000
// 并行
sum.core=> (time (parallel-sum numbers))
"Elapsed time: 133.194588 msecs"
49999995000000
sum.core=> (time (parallel-sum numbers))
"Elapsed time: 112.289406 msecs"
49999995000000
sum.core=> (time (parallel-sum numbers))
"Elapsed time: 133.077528 msecs"
49999995000000
sum.core=> (time (parallel-sum numbers))
"Elapsed time: 112.733213 msecs"
49999995000000
p.s. 我电脑上时间差异不大,可能是矢量不够大
函数式map
sum.core=> (def counts {"apple" 2 "orange" 1})
#'sum.core/counts
sum.core=> (get counts "apple" 0)
2
sum.core=> (get counts "banana" 0)
0
sum.core=> (assoc counts "banana" 1)
{"apple" 2, "orange" 1, "banana" 1}
sum.core=> (assoc counts "apple" 3)
{"apple" 3, "orange" 1}
sum.core=>
sum.core=> (frequencies ["one" "potato" "two" "potato" "three" "potato" "four"])
{"one" 1, "potato" 3, "two" 1, "three" 1, "four" 1}
sum.core=>
插播一些与序列相关的函数
sum.core=> (map inc [0 1 2 3 4 5])
(1 2 3 4 5 6)
sum.core=> (map (fn [x] (* 2 x)) [0 1 2 3 4 5])
(0 2 4 6 8 10)
sum.core=> (def multiply-by-2 (partial * 2))
#'sum.core/multiply-by-2
sum.core=> (multiply-by-2 3)
6
sum.core=> (map (partial * 2) [0 1 2 3 4 5])
(0 2 4 6 8 10)
sum.core=> (defn get-words [text] (re-seq #"\w+" text))
#'sum.core/get-words
sum.core=>
sum.core=> (get-words "one two three four")
("one" "two" "three" "four")
sum.core=>
sum.core=> (map get-words ["one two three" "four five six" "seven eight nine"])
(("one" "two" "three") ("four" "five" "six") ("seven" "eight" "nine"))
3.3 第二天:函数式并行
未完待续。。。