anon func syntax confusion

76 views
Skip to first unread message

László Török

unread,
Jan 24, 2011, 7:59:09 AM1/24/11
to clo...@googlegroups.com
Hi,

just a quick one:

(mapcat #([% 1]) [1 3 5])

gives an exception (wrong argument)

(mapcat (fn [a] [a 1]) [1 3 5])

works though...

What did I get wrong?

Thx

Las

Meikel Brandmeyer

unread,
Jan 24, 2011, 8:06:45 AM1/24/11
to Clojure
Hi,
#([% 1]) is equivalent to (fn [a] ([a 1])). Note the extra parens. You
want #(vector % 1).

Sincerely
Meikel

Nick Zbinden

unread,
Jan 24, 2011, 8:07:32 AM1/24/11
to Clojure
That a problem people find often.

The thing is #(%) not the same as (fn [x] x) its more like (fn [x]
(x)).

So if you write #([x]) is (fn [x] ([x])) witch throws an error. You
would have to write #(vec %).

Karl Ward

unread,
Jan 24, 2011, 8:11:34 AM1/24/11
to clo...@googlegroups.com

On Monday, January 24, 2011 12:59:09 PM UTC, Las wrote:
What did I get wrong?

I believe #([% 1]) is equivalent to (fn [a] ([a 1])), not (fn [a] [a 1]) . Thus, for #([% 1]) , [% 1] is called as a function. Vectors are functions that take an index and return the vector's value at that index.

László Török

unread,
Jan 24, 2011, 8:13:22 AM1/24/11
to clo...@googlegroups.com
Thanks! :)

2011/1/24 Nick Zbinden <nic...@gmail.com>
--
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

Reply all
Reply to author
Forward
0 new messages