(k...">

Why (keys) return zero in Clojure, while (keys "abc") is an error?

I am new to Clojure. The keys behavior seems inconsistent to me:

user=> (keys "")
nil
user=> (keys "abc")
ClassCastException

Empty collections appear to be handled on purpose, and test cases show that this is intentional. What is the thinking behind this behavior?

+5
source share
1 answer

The reason for this is that when a collection is converted to a sequence using a function seq, if the collection is empty, it seqreturns zero, not an empty sequence. There is another topic that discusses the reasons for this.

+2
source

All Articles