This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-26
Channels
- # babashka (5)
- # beginners (21)
- # cider (26)
- # clojure (46)
- # clojure-art (3)
- # clojure-dev (36)
- # clojure-europe (24)
- # clojure-norway (64)
- # conjure (28)
- # cursive (2)
- # datomic (1)
- # honeysql (12)
- # improve-getting-started (5)
- # introduce-yourself (5)
- # kaocha (1)
- # malli (17)
- # polylith (36)
- # reitit (3)
- # shadow-cljs (8)
Should the docstring of empty?
change, since the advice to use (seq ...)
as a replacement for (not (empty? ..))
no longer holds for counted?
collections that aren't seqable in clojure 1.12?
clj-kondo warns against (not (empty? ...))
because of the docstring in empty?
. I think discussions about this could be ended once and for all if core has a not-empty?
function.
I know there is not-empty
as well but this isn't updated to do what empty?
in clojure 1.12 does and doesn't return a boolean which is what some people want (https://twitter.com/ryrobes/status/1595923346700599298).
it did change slightly, but seq
is still the preferred idiom in seq contexts (which is how the docstring now reads)
http://clojure.github.io/clojure/branch-master/clojure.core-api.html#clojure.core/empty?
perhaps the linter could be more nuanced in this case, and only apply if x is a seq (and not a seqable)?
like if x is a vector, then it is seqable, but it is not a seq
so in the case of (not (empty? a-vector)), maybe this warning no longer applies
but if you have (not (empty? (filter ...))) it would
this is why I think a built-in not-empty?
that did the right thing always would help
I would still say you should use seq
in a seq context, even if not-empty?
existed
the "right thing" is contextual and depends on semantics
I'll try to make the linter more sophisticated. it could warn only when the argument is already a seq (and not a counted)
but I still have a hard time convincing people running into this about the importance of it 😅
I made an issue for it here earlier: https://github.com/clj-kondo/clj-kondo/issues/1743
isn't (seq already-a-seq)
just obviously better in being more concise?
I have experienced that people don't find this obviously better - e.g. read the tweet I linked in the original post
that person is wrong /shrug
I'm assuming that tweet is referring to code where this is a conditional pred, and imho seq
is absolutely the right thing to call in that circumstance both aesthetically and for performance (assuming x is a seq)
well, since it's in the docstring, I'll leave it in clj-kondo. I'm a happy seq
user but even in core libraries I've seen usages of (not (empty? ...))
it has always bugged me (pre 1.12) to call seq in this circumstance when x is not already a seq. doing seq coercion is not necessarily fast for an arbitrary coll
and those are cases where I would call (not (empty? )) or (= 0 (count x))
zero? is actually optimized so that's better
or I would use empty? and flip the cases
I think empty? is semantically the cleanest thing to say for a collection