seq

added
1.0

ns
clojure.core

type
function

(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.

                (seq '(1))  ;;=> (1)
(seq [1 2]) ;;=> (1 2)
(seq "abc") ;;=> (\\a \\b \\c)

;; Corner cases
(seq nil)   ;;=> 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 "value 2"] [:key1 "value 1"])
            
                Here is the difference between seq and sequence

user> (seq nil)
;;=> nil

user> (seq ())
;;=> nil

user> (sequence ())
;;=> ()

user> (sequence nil)
;;=> ()