What's the idiomatic Clojure for "foo = bar || baz"?

clojure, idioms

Solution

This will assign `bar` unless it is `nil` or `false`, and `baz` otherwise.

(def foo (or bar baz))

EDIT

If you wish to check for `nil` precisely, you can slightly optimize your original code, like this:

(def foo (if (nil? bar) baz bar))

I believe, this is the shortest possible way, though not idiomatic.

Problem

I want to supply a default value which can be overridden. I know I can use a ternary, like this: ``` (def foo (if (not (nil? bar)) bar baz)) ``` But surely there is a more idiomatic way in Clojure to say "use bar, or baz if bar is nil. Any suggestions?

Original source