Clojure not nil check

In Clojure nil? the nil value is checked. How to check not zero?

I want to make the Clojure equivalent of the following Java code:

 if (value1==null && value2!=null) { } 

Follow-up: I was hoping that the check was not zero, not not . if has an analog if-not . Is there such an analogue for nil? ?

+42
clojure
Aug 07 2018-12-21T00:
source share
8 answers

Another way to determine not-nil? will use the complement function, which simply inverts the likelihood of a logical function:

 (def not-nil? (complement nil?)) 

If you have multiple values ​​to check, use not-any? :

 user> (not-any? nil? [true 1 '()]) true user> (not-any? nil? [true 1 nil]) false 
+43
Aug 07 2018-12-12T00:
source share

After Clojure 1.6, can you use some? :

 (some? :foo) => true (some? nil) => false 

This is useful, for example, as a predicate:

 (filter some? [1 nil 2]) => (1 2) 
+55
Jun 04 '14 at 17:05
source share

If you are not interested in distinguishing false from nil , you can simply use this value as a condition:

 (if value1 "value1 is neither nil nor false" "value1 is nil or false") 
+15
Aug 08 '12 at 3:11
source share

In Clojure, nil is considered false for conditional expressions .

As a result, (not x) actually works in exactly the same way as (nil? x) in most cases (with the exception of logical false). eg.

 (not "foostring") => false (not nil) => true (not false) ;; false is the only non-nil value that will return true => true 

So, to answer your initial question, you can simply do:

 (if (and value1 (not value2)) ... ...) 
+13
Aug 10 '12 at 3:21
source share

condition: (and (nil? value1) (not (nil? value2)))

if-condition: (if (and (nil? value1) (not (nil? value2))) 'something)

EDIT: Does Charles Duffy give the correct user-defined definition for not-nil? :

Do you want a non-zero? Easy done: (def not-nil? (comp not nil?))

+4
Aug 07 2018-12-12T00:
source share

You can try sometime :

 user> (when-not nil (println "hello world")) =>hello world =>nil user> (when-not false (println "hello world")) =>hello world =>nil user> (when-not true (println "hello world")) =>nil user> (def value1 nil) user> (def value2 "somevalue") user> (when-not value1 (if value2 (println "hello world"))) =>hello world =>nil user> (when-not value2 (if value1 (println "hello world"))) =>nil 
+4
Nov 29 '12 at 12:48
source share

If you want your test to return true if false , you need one of the other answers. But if you just want to check that it returns a true value whenever it passed something other than nil or false , you can use identity . For example, to cut nil (or false s) from a sequence:

 (filter identity [1 2 nil 3 nil 4 false 5 6]) => (1 2 3 4 5 6) 
+4
Mar 13 '14 at 14:51
source share

If you want to use the not-nil? function not-nil? , I would suggest simply defining it as follows:

 (defn not-nil? (^boolean [x] (not (nil? x))) 

Having said that, it is worth comparing the use of this with an obvious alternative:

 (not (nil? x)) (not-nil? x) 

I am not sure that introducing an additional non-standard function is worth preserving two characters / one level of nesting. That would make sense if you wanted to use it in higher order functions, etc.

0
Aug 08 2018-12-12T00:
source share



All Articles