What is the difference between seq and seq?

------------------------- clojure.core/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. ------------------------- clojure.core/seq? ([x]) Return true if x implements ISeq ----- 

Obviously empty? based on seq. what is the difference between empty? and zero? I am confused at the impasse.

 clojure.core/empty? ([coll]) Returns true if coll has no items - same as (not (seq coll)). Please use the idiom (seq x) rather than (not (empty? x)) 

And further:

 (not (seq? ())) ;;false (not (seq ())) ;;true (not nil) ;;true 
+4
source share
1 answer
  • seq converts the collection into a sequence and returns nil if the collection is empty; also returns nil if the argument is nil.
  • seq? returns true if the argument is a sequence (implements the ISeq interface).
  • empty? will return true if the argument is either zero or an empty collection.
  • nil? will return true if the argument is nil.

I assume the bit about idiom (seq x) in docstring for empty? applies to the usual practice of using if-let as follows:

 (defn print-odd-numbers [coll] (if-let [x (seq (filter odd? coll))] (println "Odd numbers:" x) (println "No odd numbers found."))) 
+10
source

All Articles