Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Clojure how to choose a value if nil

Tags:

clojure

In Clojure what is the idiomatic way to test for nil and if something is nil then to substitute a value?

For example I do this a lot:

 let [ val    (if input-argument input-argument "use default argument")] 

: but I find it repetitive having to use "input-argument" twice.

like image 514
yazz.com Avatar asked Apr 17 '11 16:04

yazz.com


People also ask

Is nil false in Clojure?

The values nil and false are treated as logically false in Clojure. All other values are treated as logically true.

What is nil Clojure?

nil. nil is a possible value of any data type in Clojure. nil has the same value as Java null. The Clojure conditional system is based around nil and false, with nil and false representing the values of logical falsity in conditional tests - anything else is logical truth.


1 Answers

just use or:

(or input-argument "default") 
like image 86
Alex Ott Avatar answered Oct 31 '22 02:10

Alex Ott