Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I map a macro in Clojure?

I tried to do the following in Clojure

(map future exprs)

To get a seq of future tokens. Unfortunately this errs because future is itself a macro, not a function.

Is there a way to make this work while still maintaining the map syntax? I'd rather not use the for macro (just as a matter of personal style).

I suppose I'm looking for a mmap or macro map.

like image 516
MRocklin Avatar asked Sep 14 '12 03:09

MRocklin


1 Answers

As already stated by Marcin, use a lambda (anonymous function)

user=> (map and [true false 0 nil])
java.lang.Exception: Can't take value of a macro: #'clojure.core/and (NO_SOURCE_FILE:1)
user=> (map #(and %) [true false 0 nil])
(true false 0 nil)

edit

By the way, if you're using map to create a bunch of tasks to be executed in other threads, you might have issues. map is lazy, and the futures won't be created until something asks for them. You would need to force it to be evaluated (which is what happens in the repl when the repl prints the result) or find some other way.

like image 183
Bill Avatar answered Oct 23 '22 06:10

Bill