Paul Carey <paul.p.ca...@gmail.com> writes:

> Hi
>
> If I apply the following function to 4 and 6
>
> (defn arith [x y]
>   (map (fn [op] [(op x y) `(~op ~x ~y)]) [+ - / *]))
>
> I'd like to get a result of the form
>
> ([10 (+ 4 6)] [-2 (- 4 6)] ...)
>
> but instead I get something like
> ([10 (#<core$_PLUS_ clojure.core$_PLUS_@4f6de641> 4 6)] ...
>
> How do I output the var as written (I'd also be happy with
> clojure.core/+)?

You pass in the values of +, -, /, and * which are functions.  So pass
in the Vars explicitly, deref them for the calls, and use their :name
metadata for quasiquoted form.

--8<---------------cut here---------------start------------->8---
user> (defn arith [x y]
            (map (fn [op] [(@op x y) `(~(:name (meta op)) ~x ~y)])
                 [#'+ #'- #'/ #'*]))
#'user/arith
user> (arith 4 6)
([10 (+ 4 6)] [-2 (- 4 6)] [2/3 (/ 4 6)] [24 (* 4 6)])
--8<---------------cut here---------------end--------------->8---

> As an aside, when playing around with quoting and unquoting, I noticed
> that the result of ('+ 3 5) is 5. I'm not sure what I would have
> expected (maybe an error?) but it wasn't the third item of the list.
> Is there any reason for this?

Symbol implements AFn and can thus be invoked.  The implementations just
call RT.get() which is a lookup for associatives.  So

  ('+ 1) == (get '+ 1)

and

  ('+ 1 :not-found) == (get '+ 1 :not-found)

Since Symbols are nothing associative, you'll always get nil in the
former case and whatever you gave as "not-found" value in the latter
case.

Well, that explains your observation, but I have no clue how that should
make sense...

Bye,
Tassilo

-- 
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