Checking for Empty Seqs in Clojure

When coming from other languages, devs often think the best way to check for an empty vector or other sequence in Clojure is to either check the count  is less than one or check for (not (empty? some-seq)) . Both will work, but are not idiomatic, meaning “Your newbness is showing.”

The idiomatic way to check for empty sequences in Clojure is to use seq . seq  returns nil  (falsy value) when a sequence is empty. Here’s an example.

The result of this code running will be as follows.

Remember, strings are sequences, so (seq "")  will also be falsy.

The moral of the story is use seq to check for empty sequences in Clojure! If you want to see a specific Clojure tutorial, please let me know on Twiter.


Leave a Reply

Your email address will not be published. Required fields are marked *