Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why "every?" has '?' where as "some" doesnt have '?' in Clojure?

Tags:

clojure

What is fundamental difference for having a '?' in every? and not in some functions of clojure?

user> (every? true? [true true false])
false

user> (some true? [true false false])
true

Thanks.

like image 465
nishnet2002 Avatar asked Jun 08 '12 17:06

nishnet2002


1 Answers

every? returns true or false, so it gets a question mark. some doesn't return a boolean, it returns "the first logically true value returned by pred", and returns nil otherwise.

Here's the lame example I came up with:

user=> (some #(if (= 0 %) 1 0)  [1 3 5 0 9])
0 

The first element in the collection gets passed into the predicate, the predicate evaluates to 0, which is logically true so some returns 0. you can see some is not returning true or false.

So every? gets a question mark because it returns true or false. some returns the value returned by pred or nil, so it doesn't get a question mark.

like image 179
Nathan Hughes Avatar answered Oct 24 '22 06:10

Nathan Hughes