Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does (keys "") return nil in Clojure, whereas (keys "abc") is an error?

I'm new to Clojure. The behaviour of keys strikes me as inconsistent:

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

Empty collections appear to be treated specially, and the test cases indicate that this is intentional. What's the thinking behind this behaviour?

like image 672
davidchambers Avatar asked Nov 03 '22 01:11

davidchambers


1 Answers

The reason for this is that when a collection is converted to a sequence using seq function, in case the collection is empty seq will return nil rather than an empty sequence. There is another thread which discuss the reason for that.

like image 194
Ankur Avatar answered Nov 09 '22 15:11

Ankur