ClojureDocs

Nav

Namespaces

and

clojure.core

Available since 1.0 (source)
  • (and)
  • (and x)
  • (and x & next)
Evaluates exprs one at a time, from left to right. If a form
returns logical false (nil or false), and returns that value and
doesn't evaluate any of the other expressions, otherwise it returns
the value of the last expr. (and) returns true.
4 Examples
user=> (and true true)
true

user=> (and true false)
false

user=> (and false false)
false

user=> (and '() '())
()

user=> (and '[] '[])
[]

user=> (and 0 1)  ; Note that this is *not* bitwise 'and'
1

user=> (and 1 0)
0
;; See examples for "if" explaining Clojure's idea of logical true
;; and logical false.
; Note that, and does not evaluate if the first value is false
user=> (and false nil)
false

user=> (and nil false)
nil

user=> (and false (println "foo"))
false

user=> (and (println "foo") false)
foo
nil

user=> (and nil nil)
nil
; From the Clojure 1.9 source code.
(defn qualified-keyword?
  "Return true if x is a keyword with a namespace"
  [x] (and (keyword? x) (namespace x) true))

; Note how the return value of and is value of the last expression.
user=> (qualified-keyword? :hi/there)
true

; If we instead define the function as:
(defn qualified-keyword?
  [x] (and (keyword? x) (namespace x)))
; we get the namespace as return value:
user=> (qualified-keyword? :hi/there)
"hi"
See Also

Evaluates exprs one at a time, from left to right. If a form returns a logical true value, or retu...

Added by dakrone

Evaluates test.

Added by jafingerhut

Takes a set of predicates and returns a function f that returns true if all of its composing predi...

Added by lunik1
2 Notes
    By , created 13.9 years ago

    Note add is a macro, so you cannot apply it. For example, there is a vector of some Boolean values [true true false true], which you want to test to see if they are all true. The code below will not work:

    (apply add [true true false true]) ;won't work
    Instead, use this:
    (every? identity [true  true false true])
    More discussion can be found at http://osdir.com/ml/clojure/2010-01/msg01242.html

    By , created 3.4 years ago

    typo: 'and' instead of 'add'?