1.3.0 permalink Arrow_down_16x16

empty?

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))

4 Examples top

  • user=> (empty? ())
    true
    user=> (empty? '(1))
    false
  • user=> (every? empty? ["" [] () '() {} #{} nil])
    true
    
    ;example of recommended idiom for testing if not empty
    user=> (every? seq ["1" [1] '(1) {:1 1} #{1}])
    true
  • user=> (drop-while empty? ["" [] "foobar"])
    ("foobar")
    
  • user=> (empty? nil)
    true
Log in to add / edit an example.

See Also top

  • 0
    clojure.core/seq

    Returns a seq on the collection. If the collection is empty, retu

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:5446 top

(defn empty?
  "Returns true if coll has no items - same as (not (seq coll)).
  Please use the idiom (seq x) rather than (not (empty? x))"
  {:added "1.0"
   :static true}
  [coll] (not (seq coll)))
Vars in clojure.core/empty?:
Used in 0 other vars

Comments top

2 comment(s) for empty?.

Some explanation of why (seq x) is preferable over (not (empty? x)) would be good. Because it's far less readable. When I come across (not (empty? x)) in some code, I immediately understand the author's intention. Not so much with (seq x).

I think this is an efficiency thing. If you expand the source you can see that empty? is equivalent to (not (seq coll)) so (not (empty? coll)) would macroexpand to (not (not (seq coll))

Log in to add a comment.