clojure.core/empty?¶
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¶
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:
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:
Note by MicahElliott¶
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¶
- clojure.core/seq (5)
- clojure.core/empty (1)
- clojure.core/not-empty (4)
Content from the matching ClojureDocs page, with authors credited on each contribution.