Welcome! Please see the About page for a little more info on how this works.

0 votes
in Syntax and reader by
edited by

Clojure's any? function seems to just return true always.
(defn any? "Returns true given any argument." {:tag Boolean :added "1.9"} [x] true)

Contrast this with not-any?.
(def ^{:tag Boolean :doc "Returns false if (pred x) is logical true for any x in coll, else true." :arglists '([pred coll]) :added "1.0"} not-any? (comp not some))

I believe any? should take two arguments and return true if any predicate is true.
(def ^{:tag Boolean :doc "Returns true if (pred x) is logical true for any x in coll, else false." :arglists '([pred coll]) :added "1.9"} any? (comp boolean some))
Credit to Kofrasa on ClojureDocs.

Here are some tests to run:

`
(ns user
(:require [clojure.test :refer [are deftest]]

        [clojure.test.check.clojure-test :refer [defspec]]
        [clojure.test.check.generators :as gen]
        [clojure.test.check.properties :as prop])

(:refer-clojure :exclude [any?]))

(deftest clojure-any
(are [x y] (= ((comp boolean some) true? x) (clojure.core/any? y))

[true] true
[false] false ;; this fails
[true true] true
[false true] true
[false false] false ;; so does this
))

(def
^{:tag Boolean
:doc "Returns true if (pred x) is logical true for any x in coll,
else false."
:arglists '([pred coll])
:added "1.9"}
any? (comp boolean some))

(defspec correct-any 1000
(prop/for-all [v (gen/such-that not-empty (gen/vector gen/boolean))]

            (= ((comp boolean some) true? v)
               (any? true? v)
               (not (not-any? true? v)))))

`

Apologies for the messed-up formatting.
https://gist.github.com/wildwestrom/9568e9c659d30a2a663a9a0a8235a6b9

1 Answer

+1 vote
by

any? is a predicate that is intended to always return true, so yes it is the correct behavior. any? was added as a predicate to use with spec in the case where any value is valid. It is not a complement to not-any?, despite that obvious assumption. There are, in the end, only so many words and despite a lot of care in this regard, there are times when these confusions exist.

Re "I believe any? should take two arguments and return true if any predicate is true.", this is similar to and (for N args) but that does have limits as a macro, or every? (for a coll), or the higher-order function every-pred for creating a composite pred. Depending on your case, one of those probably makes sense.

by
Got it, should I forward this explanation to ClojureDocs?
by
If you like!
by
Thank you very much!
by
No competent language standards committee would accept a definition of `any?` that did not equate to `(complement not-any?)`. This is a peek into the lamentable state of governance of Clojure: the best language to fail since Smalltalk, if not forever.
by
Sounds like you've never worked on a language standards committee, Chastie? I spent eight years on the ANSI C++ Committee (and was its secretary for three years). Standards committees have to accept all sorts of compromises and, as Alex says, there are only a limited number of words available for certain constructs.
...