Hello All,
I am new to Clojure. Surprised why this code does not work:
user=> (filter #(%) [1 2 3])
ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn
Here my intent behind #(%) is to define a lambda function returning
its argument. Since Clojure defines truth on any type, it should be
acceptable as filter function, and the result should be the original
array
Some narrowing down. What's #(%) really good for?
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
> For something like "=> true" try:
> user=> (defmacro => [expr] `(fn [] ~expr))
> #'user/=> (macroexpand-1 '(=> true))
> (clojure.core/fn [] true)
Alternatively: (constantly true)
Have you tried (constantly true)? The main difference is that it will
ignore all arguments, but perhaps it's a more concise way of
expressing what you want.
-Phil
This actually is trying to call 'true' as if it were a function, not a
constant. The thing I think you're missing here is: when a symbol is
butted up against an opening parenthesis, it is treated as a function
call, with the remaining elements in the list as arguments (unless the
list is quoted, in which case it is not evaluated and treated as just
a list).
#(true) says call 'true' as a function with no arguments. #(%) says
call the argument passed to this anonymous function as a function, and
if, for example you were mapping a vector of numbers to this anonymous
function, then each number would be called as a function.
As you've already seen, if you just want the original value returned
from a function, you can call the 'identity' function with that
something as an argument, as in #(identity %) or #(identity true),
etc.
I should have also made clear here that you would never actually use
this in real code, just for learning. See the other responses for the
actual ways to do what you are trying to do.