On Wed, Nov 2, 2011 at 5:37 AM, Dennis Haupt <d.haup...@googlemail.com> wrote:
> i stumbled over two odd things
> 1) -> and ->> have the same source code. why is that?

-> and ->> are macros that have similar purposes. "->" inserts the previous form
as the first argument to the function, "->>" inserts it as the last.


> 2) why can't i use "(-> "hi" #(println %))" directly? why do i have to
> put my function into a symbol first? is there a way to avoid this?

This is one is a bit more involved.  The #(func ...) form is a reader
macro so it gets
translated before macro expansion occurs:

user> (read-string
        "(-> \"hi\" #(println %))")
(-> "hi" (fn* [p1__2134#] (println p1__2134#)))

The result is that your initial string is inserted into a lambda
expression in the wrong place (where the parameter list should be).
When clojure tries to compile the resulting form after macro
expansion, you get an error:

user> (macroexpand '(-> "hi" (fn* [p1__2134#] (println p1__2134#))))
(fn* "hi" [p1__2134#] (println p1__2134#))
     ####

The easiest fix if you need to use inline function definitions is to
wrap them in parens:

(-> "hi" (#(println %)))

Although, in this case it's not necessary, it would have been easier to do

(-> "hi" println)

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to