Using OR in a call to reduce


#1

Hi,

I just came across a situation where I wanted to use or in a reduce call:

(reduce or false [false true true])

and found out that this is not possible with or:

CompilerException java.lang.RuntimeException: Can't take value of a macro: #'clojure.core/or, compiling:(/tmp/form-init4325941840622475340.clj:1:1) 

To make it work I have to wrap it in an anonymous function like this:

(reduce #(or %1 %2) false [false true true])

I would like to know why the first statement is not possible? Does the error message mean that would not be possible with any macro?

Thanks,
Torsten.


#2

Hi Torsten!

Yeah, I think you got it right. Macros actually have no value at runtime. In Clojure, they throw that exception that you showed. The only place you can refer to a macro is in the first position of an application. The reason is that it gets complicated to define what it would mean to apply a macro at runtime, so the possibility is avoided altogether. Many papers have been written about it and it just doesn’t seem to have a utility to justify the complexity.

The way you worked around the situation is the right way to do it. It’s kind of unfortunate, but it’s the way it is.

Eric


#3

Thanks Eric, for the confirmation.

In the end I could wrap it myself in a named function and then use that instead of the anonymous fn if I had to use it multiple times…

Thanks,
Torsten


#4

You might want to check the some function as well to find a truthy value.


#5

Thanks for the tip! I used some in a number of cases, though it never occurred to me to use it in this case :smile: