Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

idiomatic way to replace (null x) function from common lisp in clojure

In Common Lisp you use the (null x) function to check for empty lists and nil values.

Most logically this maps to

(or (nil?  x) (= '() x))

In clojure. Can someone suggest a more idiomatic way to do it in Clojure?

like image 631
hawkeye Avatar asked Jul 13 '10 13:07

hawkeye


1 Answers

To get the same result for an empty list in Clojure as you do in Common Lisp, use the empty? function. This function is in the core library: no imports are necessary.

It is also a predicate, and suffixed with a ?, making it a little clearer what exactly you're doing in the code.

=> (empty? '())
true
=> (empty? '(1 2))
false
=> (empty? nil)
true

As j-g faustus already noted, seq can be used for a similar effect.

like image 86
Isaac Avatar answered Jan 23 '23 03:01

Isaac