Skip to content

clojure.core/empty?

View this page on ClojureDocs

Type: function Added: Clojure 1.0 Examples: 5 Runnable: 5
([coll])

Returns true if coll has no items. To check the emptiness of a seq, please use the idiom (seq x) rather than (not (empty? x))

Examples

by dpritchett on
by nipra on
by Jacolyte on
by ryo on
by Jjunior130 on

Note by cap10morgan

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

Note by phreed

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

Note by moea

Avoiding (not (empty? s)) because it macroexpands into (not (not ...)), on the grounds of efficiency, is ridiculous.

I'm a fan of:

(when (not-empty s)
  ...)

Note by cmal

If I just want to return false when empty, true when not empty, then what can I use if I am going to avoid (not (empty? ...))?

Note by yogsototh

Just note (not (empty? x)) is not perfectly equivalent to seq:

user=> (seq '(1 2))
(1 2)
user=> (not (empty? '(1 2)))
true

Note by MicahElliott

user> (let [r (and (not (empty? [])))] r)
;; => false
user> (let [r (and (seq []))] r)
;; => nil

Note by mars0i

(seq x) is considered idiomatic, so in theory it should be understood by experienced Clojurians. (not (empty? x)) might be better when you expect your code to be read by those who don't fit that description. (However, there are a number of Clojure idioms that could be confusing to those without much experience; avoiding them all might not be desirable.)

See also


Content from the matching ClojureDocs page, with authors credited on each contribution.