(def counts {"apple" 2 "orange" 1}) => (get counts "apple" 0) //2 => (get counts "apples" 0) //0 => (assoc counts "banana" 1) //{"apple" 2, "orange" 1, "banana" 1} => (assoc counts "apple" 3) //{"apple" 3, "orange" 1}注意是沒有上面加的banana的哦
=> (map inc [0 1 2 3 4 5]) //(1 2 3 4 5 6) => (map (fn [x] ( * 2 x )) [0 1 2 3 4 5]) //(0 2 4 6 8 10)
=> (def multiply-by-2 ( partial * 2 )) //(multiply-by-2 3)是6 => (map (partial * 2 ) [0 1 2 3 4 5]) //(0 2 4 6 8 10)
user=> (defn get-words [text] (re-seq #"\w+" text)) user=> (get-words "one two three four") #("one" "two" "three" "four") user=> (map get-words ["one two three" "4 5 6" "seven eight nine"]) #(("one" "two" "three") ("4" "5" "6") ("seven" "eight" "nine")) #若是須要包含全部輸出的一維序列,可使用mapcat user=> (mapcat get-words ["one two three" "4 5 6" "seven eight nine"]) #("one" "two" "three" "4" "5" "6" "seven" "eight" "nine")
(defn recursive-sum [numbers] (if (empty? numbers) 0 (+ (first numbers) (recursive-sum (rest numbers)))))
(defn reduce-sum [numbers] (reduce (fn [acc x] (+ acc x)) 0 numbers))
(defn sum [numbers] (reduce + numbers))
(ns sum.core (:require [clojure.core.reducers :as r])) (defn parallel-sum [numbers] (r/fold + numbers))
user=> (take 10 (iterate inc 0)) user=> (take 10 (iterate (partial + 2) 2)) # (2 4 6 8 10 12 14 16 18 20)
user=> (def pages ["one potato two potato three potato four" "five potato six potato seven potato more"]) user=> (defn get-words [text] (re-seq #"\w+" text)) user=> (pmap #(frequencies (get-words %)) pages) # ({"one" 1, "potato" 3, "two" 1, "three" 1, "four" 1} {"five" 1, "potato" 3, "six" 1, "seven" 1, "more" 1})
(defn count-words-parallel [pages] (reduce (partial merge-with +) (pmap #(frequencies (get-words %)) pages))) # 使用 user=> (count-words-parallel pages) {"one" 1, "potato" 6, "two" 1, "three" 1, "four" 1, "five" 1, "six" 1, "seven" 1, "more" 1}
上面的程序加速比1.5,並不理想,緣由仍是逐頁的進行計數合併java
利用partition-all函數,能夠將一個序列中的元素分批,構成多個序列 eg.正則表達式
user=> (partition-all 4 [1 2 3 4 5 6 7 8 9 10]) ((1 2 3 4) (5 6 7 8) (9 10))
(defn count-words [pages] (reduce (partial merge-with +) (pmap count-words-sequential (partition-all 100 pages))))
user=>user=> (require '[clojure.core.reducers :as r]) nil user=> (r/map (partial * 2) [1 2 3 4]) #object[clojure.core.reducers$folder$reify__107 0x648c94da "clojure.core.reducers$folder$reify__107@648c94da"]
user=> (reduce conj [] (r/map (partial * 2) [1 2 3 4])) #[2 4 6 8]
user=> (into [] (r/map (partial * 2) [1 2 3 4])) #[2 4 6 8]
在純粹的函數式語言中,每一個函數都具備引用透明性-在任何調用函數的地方,均可以調用函數運行的結果來替換函數的調用,而不會對程序產生反作用.編程
user=> (def sum ( future (+ 1 2 3 4 5 ))) user=> sum #object[clojure.core$future_call$reify__6962 0x561868a0 {:status :ready, :val 15}] user=>user=> (deref sum) 15 user=>user=> @sum 15
user=> (let [a (future (+ 1 2)) b (future (+ 3 4))] (+ @a @b)) #10
user=> (def meaning-of-life (promise)) user=> (future (println "the meaning of life is:" @meaning-of-life)) #object[clojure.core$future_call$reify__6962 0x6a55299e {:status :pending, :val nil}] user=>user=> (deliver meaning-of-life 42) the meaning of life is: 42 #object[clojure.core$promise$reify__7005 0x289710d9 {:status :ready, :val 42}]