let Create a new scope
1 2 3 4 (let [[part & remaining] remaining-asym-parts] (recur remaining (into final-body-parts (set [part (matching-part part)]))))
loop 1 2 3 4 5 6 7 8 9 10 11 (loop [iteration 0 ] (println (str "Iteration " iteration)) (if (> iteration 3 ) (println "Goodbye!" ) (recur (inc iteration))))
cond 1 2 3 4 5 6 7 (defn pos-neg-or-zero "Determines whether or not n is positive, negative, or zero" [n] (cond (< n 0 ) "negative" (> n 0 ) "positive" :else "zero" ))
Regular Expressions re-find
Better Symmetrizer with reduce 1 2 3 4 5 6 like this (+ (+ (+ 1 2 ) 3 ) 4 ) you can (reduce + 15 [1 2 3 4 ])
into repeat 1 2 3 4 5 (def websites (into [](repeat 1000 "codewars" )) )
seq Returns a seq on the collection. If the collection is empty, returns nil. (seq nil) returns nil. seq also works on Strings, native Java arrays (of reference types) and any objects that implement Iterable.
1 2 3 4 5 6 7 8 (seq '(1 2 3 )) (seq [1 2 3 ]) (seq #{1 2 3 }) (seq {:name "Bill Compton" :occupation "Dead mopey guy" })
map 1 2 3 4 (map inc [1 2 3]); => (2 3 4) (map str ["a" "b" "c"] ["A" "B" "C"]); => ("aA" "bB" "cC")
reduce
take 1 2 3 4 (take 3 [1 2 3 4 5 6 7 8 9 10]) ; => (1 2 3) (drop 3 [1 2 3 4 5 6 7 8 9 10]) ; => (4 5 6 7 8 9 10)
take-while 1 2 3 4 5 (take-while #(< (:month %) 3) food-journal) ; => ({:month 1 :day 1 :human 5.3 :critter 2.3} {:month 1 :day 2 :human 5.1 :critter 2.0} {:month 2 :day 1 :human 4.9 :critter 2.1} {:month 2 :day 2 :human 5.0 :critter 2.5})
drop-while 1 2 3 4 5 (drop-while #(< (:month %) 3) food-journal) ; => ({:month 3 :day 1 :human 4.2 :critter 3.3} {:month 3 :day 2 :human 4.0 :critter 3.8} {:month 4 :day 1 :human 3.7 :critter 3.9} {:month 4 :day 2 :human 3.7 :critter 3.6})
filter 1 2 3 4 5 6 (filter #(< (:human %) 5) food-journal) ; => ({:month 2 :day 1 :human 4.9 :critter 2.1} {:month 3 :day 1 :human 4.2 :critter 3.3} {:month 3 :day 2 :human 4.0 :critter 3.8} {:month 4 :day 1 :human 3.7 :critter 3.9} {:month 4 :day 2 :human 3.7 :critter 3.6})
some a collection contains any values that test true for a predicate function
1 2 3 4 (some #(> (:critter %) 5 ) food-journal) (some #(> (:critter %) 3 ) food-journal)
sort and sort-by 1 2 3 4 (sort [3 1 2]) ; (1 2 3) (sort-by count ["aaa" "c" "bb"]) ; => ("c" "bb" "aaa")
concat 1 2 (cancat [1 2] [3 4]) ; => (1 2 3 4)
lazy-seq A lazy seq is a seq whose members aren’t computed until you try to access them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 (def vampire-database {0 {:makes-blood-puns? false, :has-pulse? true :name "McFishwich"} 1 {:makes-blood-puns? false, :has-pulse? true :name "McMackson"} 2 {:makes-blood-puns? true, :has-pulse? false :name "Damon Salvatore"} 3 {:makes-blood-puns? true, :has-pulse? true :name "Mickey Mouse"}}) (defn vampire-related-details [social-security-number] (Thread/sleep 1000) (get vampire-database social-security-number)) (defn vampire? [record] (and (:makes-blood-puns? record) (not (:has-pulse? Record)) record)) (defn identify-vampire [social-security-numbers] (first (filter vampire? (map vampire-related-details social-security-numbers))))
Infinite Sequences repeat 1 2 3 4 5 6 7 8 9 10 (concat (take 8 (repeat "na")) ["Batman!"]) ; => ("na" "na" "na" "na" "na" "na" "na" "na" "Batman!") (take 3 (repeatedly (fn [] (rand-int 10)))) ; => (1 4 0) (defn even-numbers ([] (even-numbers 0)) ([n] (cons n (lazy-seq (even-numbers (+ n 2)))))) (take 10 (even-numbers)); => (0 2 4 6 8 10 12 14 16 18) (cons 0 '(2 4 6)); => (0 2 4 6)
The Collection Abstraction The collection abstraction is closely related to the sequence abstraction.
empty 1 2 3 4 (empty? []) ; => true (empty? ["no!"]) ; => false
into 1 2 3 4 5 (map identity {:sunlight-reaction "Glitter!"}) ; => ([:sunlight-reaction "Glitter!"]) (into {} (map identity {:sunlight-reaction "Glitter!"})) ; => {:sunlight-reaction "Glitter!"}
conj 1 2 (conj [0] [1]) ; => [0 [1]]
Function Functions apply 1 2 3 4 5 6 7 8 (max 0 1 2) ; => 2 (max [0 1 2]) ; => [0 1 2] (apply max [0 1 2]) ; => 2
partial 像一个闭包一样保存着环境
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 (def add10 (partial + 10)) (add10 3) ; => 13 (add10 5) ; => 15 (def add-missing-elements (partial conj ["water" "earth" "air"])) (add-missing-elements "unobtainium" "adamantium") ; => ["water" "earth" "air" "unobtainium" "adamantium"] (defn lousy-logger [log-level message] (condp = log-level :warn (clojure.string/lower-case message) :emergency (clojure.string/upper-case message))) (def warn (partial lousy-logger :warn)) (warn "Red light ahead"); => "red light ahead"
slurp 读取文件的函数