You're viewing version 1.2.0 of complement. The latest stable version of Clojure Core is 1.3.0.
1.2.0 Arrow_down_16x16

complement

clojure.core

  • (complement f)
Takes a fn f and returns a fn that takes the same arguments as f,
has the same effects, if any, and returns the opposite truth value.

1 Example top

  • ;; a simple not-empty? predicate
    user=> (def not-empty? (complement empty?))
    #'user/not-empty?
    user=> (not-empty? [])
    false
    user=> (not-empty? [1 2])
    true
    
    
    ;; a slightly more complex example
    ;; this function takes two arguments, and sometimes returns nil
    
    user=> (defn contains-char? [the-string, the-char]
             (some #(= the-char %) the-string))
    #'user/contains-char?
    
    user=> (contains-char? "abc" \b)
    true
    user=> (contains-char? "abc" \j)
    nil
    
    ;; define the complement, to check if a char is absent
    user=> (def does-not-contain-char? (complement contains-char?))
    #'user/does-not-contain-char?
    
    ;; our complement does exactly what we expect
    user=> (does-not-contain-char? "abc" \b)
    false
    user=> (does-not-contain-char? "abc" \j)
    true
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:1101 top

(defn complement
  "Takes a fn f and returns a fn that takes the same arguments as f,
  has the same effects, if any, and returns the opposite truth value."
  {:added "1.0"}
  [f] 
  (fn 
    ([] (not (f)))
    ([x] (not (f x)))
    ([x y] (not (f x y)))
    ([x y & zs] (not (apply f x y zs)))))
Vars in clojure.core/complement: apply defn fn not

Comments top

No comments for complement. Log in to add a comment.