Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't (apply or [true false]) work in Clojure?

Tags:

From what I understand about apply, it unpacks a list and turns the elements into arguments for a function.

I see that (apply + [1 2 3]) works as expected, i.e: it's equivalent to (+ 1 2 3).

Why then is (apply or [true false]) invalid? Isn't it equivalent to (or true false) ?

like image 990
Tachy Avatar asked Jun 03 '10 20:06

Tachy


2 Answers

Because or is a macro, not a normal function. You can get the same effect with (some identity [true false]).

like image 191
Chuck Avatar answered Oct 13 '22 00:10

Chuck


As an alternative to or you can use (some predicate coll).

clojure.core/some ([pred coll])
Returns the first logical true value of (pred x) for any x in coll, else nil. One common idiom is to use a set as pred, for example this will return :fred if :fred is in the sequence, otherwise nil: (some #{:fred} coll)

like image 37
Bozhidar Batsov Avatar answered Oct 12 '22 23:10

Bozhidar Batsov