• 抽象
    • Sequence Abstraction
    • First
    • Rest
    • Cons
    • Collection Abstraction
    • into

    抽象

    在了解sequence之前,我们可以先了解下abstractionabstraction的概念在很多语言里面都有,譬如Gointerface就是abstraction:

    1. type IA interface {
    2. DoFunc()
    3. }
    4. type A struct {}
    5. func (a *A) DoFunc() {
    6. }

    在上面这个例子中,struct A实现了DoFunc函数,我们就可以认为,A实现了IA

    Sequence Abstraction

    Clojure也提供了abstraction的概念,这里我们主要来了解下sequence abstraction

    Clojure里面,如果这些core sequence function firstrestcons能够用于某个data structure,我们就可以认为这个data structure实现了sequence abstraction,就能被相关的sequence function进行操作,譬如mapreduce等。

    First

    first返回collection里面的第一个元素,譬如:

    1. user=> (first [1 2 3])
    2. 1
    3. user=> (first '(1 2 3))
    4. 1
    5. user=> (first #{1 2 3})
    6. 1
    7. user=> (first {:a 1 :b 2})
    8. [:a 1]

    Rest

    rest返回collection里面,第一个元素后面的sequence,譬如:

    1. user=> (rest [1 2 3])
    2. (2 3)
    3. user=> (rest [1])
    4. ()
    5. user=> (rest '(1 2 3))
    6. (2 3)
    7. user=> (rest #{1 2 3})
    8. (3 2)
    9. user=> (rest {:a 1 :b 2})
    10. ([:b 2])

    Cons

    Cons则是将一个元素添加到collection的开头,譬如:

    1. user=> (cons 1 [1 2 3])
    2. (1 1 2 3)
    3. user=> (cons 1 '(1 2 3))
    4. (1 1 2 3)
    5. user=> (cons 1 #{1 2 3})
    6. (1 1 3 2)
    7. user=> (cons 1 {:a 1 :b 2})
    8. (1 [:a 1] [:b 2])
    9. user=> (cons {:c 3} {:a 1 :b 2})
    10. ({:c 3} [:a 1] [:b 2])

    从上面的例子可以看出,Clojure自身的vectorlist等都实现了sequence abstraction,所以他们也能够被一些sequence function调用:

    1. user=> (defn say [name] (str "hello " name))
    2. #'user/say
    3. user=> (map say [1 2])
    4. ("hello 1" "hello 2")
    5. user=> (map say '(1 2))
    6. ("hello 1" "hello 2")
    7. user=> (map say #{1 2})
    8. ("hello 1" "hello 2")
    9. user=> (map say {:a 1 :b 2})
    10. ("hello [:a 1]" "hello [:b 2]")
    11. user=> (map #(say (second %)) {:a 1 :b 2})
    12. ("hello 1" "hello 2")

    Collection Abstraction

    sequence abstraction类似,Clojure里面的core data structure,譬如vectorlist等,都实现了collection abstraction

    Collection abstraction通常是用于处理整个data structure的,譬如:

    1. user=> (count [1 2 3])
    2. 3
    3. user=> (empty? [])
    4. true
    5. user=> (every? #(< % 3) [1 2 3])
    6. false
    7. user=> (every? #(< % 4) [1 2 3])
    8. true

    into

    一个重要的collection function就是intosequence function通常会返回一个seq,而into会将返回的seq转换成原始的data structure,譬如:

    1. user=> (into [] [1 2 3])
    2. [1 2 3]
    3. user=> (into [] (map inc [1 2 3]))
    4. [2 3 4]