ClojureDocs

导航

命名空间

seq

clojure.core

自 1.0 起可用 ()
  • (seq coll)
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. Note that seqs cache values, thus seq
  should not be used on any Iterable whose iterator repeatedly
  returns the same mutable object.
5 Examples
(seq '(1))  ;;=> (1)
(seq [1 2]) ;;=> (1 2)
(seq "abc") ;;=> (\a \b \c)

;; Corner cases
(seq nil)   ;;=> nil
(seq '())   ;;=> nil
(seq [])    ;;=> nil
(seq "")    ;;=> nil
;; (seq x) is the recommended idiom for testing if a collection is not empty
(every? seq ["1" [1] '(1) {:1 1} #{1}])
;;=> true
;; 'seq' can be used to turn a map into a list of vectors.
;; Notice how the list is built adding elements to the beginning 
;; of the seq (list) not to the end, as with vectors.
;; (Of course, the order that items are 
;;  taken from a map should not be relied upon
;;  unless a deterministic 'sorted-map' is used.)
(seq {:key1 "value1" :key2 "value2"})
;;=> ([:key2 "value2"] [:key1 "value1"])
;; Here is the difference between seq and sequence

(seq nil)
;;=> nil

(seq ())
;;=> nil

(sequence ())
;;=> ()

(sequence nil)
;;=> ()
;; seq is very similar to not-empty:

(every? seq ["1" [1] '(1) {:1 1} #{1}])
;;=> true
(every? not-empty ["1" [1] '(1) {:1 1} #{1}])
;;=> true

(seq '(1))       ;;=> (1)
(not-empty '(1)) ;;=> (1)

(seq [1 2])       ;;=> (1 2)
(not-empty [1 2]) ;;=> [1 2]


(seq "abc")       ;;=> (\a \b \c)
(not-empty "abc") ;;=> "abc"

(map seq [nil '() [] "" {}])
;;=> (nil nil nil nil nil)

(map not-empty [nil '() [] "" {}])
;;=> (nil nil nil nil nil)
See Also

Return true if x implements ISeq

Added by mmwaikar

Returns true if coll has no items - same as (not (seq coll)). Please use the idiom (seq x) rather ...

Added by dansalmo

Returns a seq on a java.util.Iterator. Note that most collections providing iterators implement It...

Added by ryo

Return true if the seq function is supported for x

Added by svenschoenung

Returns true if coll implements Sequential

Added by MicahElliott

Coerces coll to a (possibly empty) sequence, if it is not already one. Will not force a lazy seq. ...

Added by MicahElliott
0 Notes
No notes for seq