user4813927
user4813927

Reputation:

every? identity in Clojure

I don't get why the combination of every? and identity is giving different results in the following two examples. It seems that it gives the expected answer upon calling it on true false collections, but not with the numbers and strings:

(every? identity [1 2 3 4])=> true

(every? identity [true true false])=> false

Upvotes: 0

Views: 553

Answers (2)

Razvanescu
Razvanescu

Reputation: 113

This is from the clojure documentation for every?:

Returns true if (pred x) is logical true for every x in coll, else false.

By evaluating (identity) on the false value inside the array you are getting false because the identity of false is false.

Upvotes: 2

Micah Elliott
Micah Elliott

Reputation: 10264

It makes sense in your latter case that every? would return false since one of the elements in the tested collection is false; i.e.:

=> (identity false)
false

As every? works its way across the vector and encounters the above application, it sees a falsy value, so returns such.

=> (doc every?)
-------------------------
clojure.core/every?
([pred coll])
  Returns true if (pred x) is logical true for every x in coll, else
  false.

Upvotes: 4

Related Questions