..

Clojure 中的 sequential collection

Clojure Sequential Collections

Vectors

  • are indexed 1
  • are represented with []
  • immutability
  • add elements at the tail
  1. indexed access

    (get ["abc" false 3] 0)
    
  2. count

    (count [1 3 "abc"])
    
  3. constructing

    (vector 1 2 3)
    
  4. add elements

    (conj [1 2 3] 4 5 6)
    

Lists

  • are sequential linked list
  • add new elements at the head of the list
  1. constructing

    (def cards '(10 :ace :jack 9)) # 包含了4个元素,因为(会被认为是方法调用,所有要加 单引号
    
  2. first

    (first cards)
    (first (rest cards))
    
  3. add

    (conj cards :quene) # add front
    
  4. stack access

    # list can also be used as a stack 
    (def stack '(:a :b))
    (peek stack) # :a
    (pop stack) # :b