Re: Map a list of agents to a list of their values

2009-11-15 Thread Meikel Brandmeyer

Hi,

Am 14.11.2009 um 20:31 schrieb John Harrop:

For situations like this, I find it handy to discover what reader- 
macros are expanding to. This works well:


user=(defmacro expand [arg] (println arg))
#'user/expand
user=(expand #(@%))
(fn* [p1__6536] ((clojure.core/deref p1__6536)))


user= (macroexpand-1 '#(@%))
(fn* [p1__13] ((clojure.core/deref p1__13)))

;)

Sincerely
Meikel



smime.p7s
Description: S/MIME cryptographic signature


Re: Map a list of agents to a list of their values

2009-11-15 Thread John Harrop
On Sat, Nov 14, 2009 at 6:03 PM, Meikel Brandmeyer m...@kotka.de wrote:

 Hi,

 Am 14.11.2009 um 20:31 schrieb John Harrop:


  For situations like this, I find it handy to discover what reader-macros
 are expanding to. This works well:

 user=(defmacro expand [arg] (println arg))
 #'user/expand
 user=(expand #(@%))
 (fn* [p1__6536] ((clojure.core/deref p1__6536)))


 user= (macroexpand-1 '#(@%))
 (fn* [p1__13] ((clojure.core/deref p1__13)))


Funny, I get
user= (macroexpand-1 '#(@%))
(fn* [p1__6540] (@p1__6540))

as apparently the Enclojure REPL turns (deref foo) back into @foo when
printing it. In fact I fiddled with the macro just because '#(@%) by itself
at my REPL just echoed itself. But this does work:

user= (prn '#(@%))
(fn* [p1__6544] ((clojure.core/deref p1__6544)))

So does temporarily clicking off pretty-printing at the REPL and just
evaluating '#(@%).

On the other hand, the macro's argument doesn't need to be quoted, so
there's one less awkward-to-type punctuation character. :)

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

Re: Map a list of agents to a list of their values

2009-11-14 Thread Sean Devlin
Try

(map deref agents)

On Nov 14, 12:49 pm, Kevin Q kevin.jing@gmail.com wrote:
 I have a list of agents, each of which has a hasmap state. I want to
 get a list of values from the list of agents, naturally I used the map
 function and print the result of the map:

 (println
   (map #(@%) agents))

 However, when I run this, I got error Wrong number of args passed to:
 PersistentHashMap

 I'm wondering why is this happening? Prior to calling the map
 function, I did (apply await agents) to wait for all my agents to
 finish. Does it affect things?

 Thanks,

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


Re: Map a list of agents to a list of their values

2009-11-14 Thread John Harrop
On Sat, Nov 14, 2009 at 12:49 PM, Kevin Q kevin.jing@gmail.com wrote:

 I have a list of agents, each of which has a hasmap state. I want to
 get a list of values from the list of agents, naturally I used the map
 function and print the result of the map:

 (println
  (map #(@%) agents))

 However, when I run this, I got error Wrong number of args passed to:
 PersistentHashMap

 I'm wondering why is this happening?


For situations like this, I find it handy to discover what reader-macros are
expanding to. This works well:

user=(defmacro expand [arg] (println arg))
#'user/expand
user=(expand #(@%))
(fn* [p1__6536] ((clojure.core/deref p1__6536)))

So it will have the same effect as (map deref agents), though the latter is
clearer.

I don't know why it's failing, but it seems unlikely that switching to (map
deref agents) will fix it.

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

Re: Map a list of agents to a list of their values

2009-11-14 Thread Kevin Q
Thanks! That works.
However, why wouldn't (map #(@%) agents) work?

On Nov 14, 1:01 pm, Sean Devlin francoisdev...@gmail.com wrote:
 Try

 (map deref agents)

 On Nov 14, 12:49 pm, Kevin Q kevin.jing@gmail.com wrote:

  I have a list of agents, each of which has a hasmap state. I want to
  get a list of values from the list of agents, naturally I used the map
  function and print the result of the map:

  (println
    (map #(@%) agents))

  However, when I run this, I got error Wrong number of args passed to:
  PersistentHashMap

  I'm wondering why is this happening? Prior to calling the map
  function, I did (apply await agents) to wait for all my agents to
  finish. Does it affect things?

  Thanks,

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


Re: Map a list of agents to a list of their values

2009-11-14 Thread Kevin Q
Hi,
Thanks for the hint. I tried (map deref agents) and it did work. I
don't know if this is a bug?

On Nov 14, 2:31 pm, John Harrop jharrop...@gmail.com wrote:
 On Sat, Nov 14, 2009 at 12:49 PM, Kevin Q kevin.jing@gmail.com wrote:
  I have a list of agents, each of which has a hasmap state. I want to
  get a list of values from the list of agents, naturally I used the map
  function and print the result of the map:

  (println
   (map #(@%) agents))

  However, when I run this, I got error Wrong number of args passed to:
  PersistentHashMap

  I'm wondering why is this happening?

 For situations like this, I find it handy to discover what reader-macros are
 expanding to. This works well:

 user=(defmacro expand [arg] (println arg))
 #'user/expand
 user=(expand #(@%))
 (fn* [p1__6536] ((clojure.core/deref p1__6536)))

 So it will have the same effect as (map deref agents), though the latter is
 clearer.

 I don't know why it's failing, but it seems unlikely that switching to (map
 deref agents) will fix it.

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


Re: Map a list of agents to a list of their values

2009-11-14 Thread John Harrop
On Sat, Nov 14, 2009 at 2:51 PM, Kevin Q kevin.jing@gmail.com wrote:

 Hi,
 Thanks for the hint. I tried (map deref agents) and it did work. I
 don't know if this is a bug?


Nah, it's just being really sneaky.


  (fn* [p1__6536] ((clojure.core/deref p1__6536)))


Even I didn't notice it before. There's an extra pair of parens, so it's
double-dereferencing. The problem is that #() brings in one pair, and @ in
expanding to (deref the-argument) brings in a second.

Just use (map deref agents).

General advice: Avoid directly using another reader macro inside #(); the
first thing should always be a symbol or a call that evaluates to a
function, e.g. #((function-returning-function arg) other-arg).

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

Re: Map a list of agents to a list of their values

2009-11-14 Thread John Harrop
On Sat, Nov 14, 2009 at 3:24 PM, John Harrop jharrop...@gmail.com wrote:

 On Sat, Nov 14, 2009 at 2:51 PM, Kevin Q kevin.jing@gmail.com wrote:

 Hi,
 Thanks for the hint. I tried (map deref agents) and it did work. I
 don't know if this is a bug?


 Nah, it's just being really sneaky.


   (fn* [p1__6536] ((clojure.core/deref p1__6536)))


 Even I didn't notice it before. There's an extra pair of parens, so it's
 double-dereferencing. The problem is that #() brings in one pair, and @ in
 expanding to (deref the-argument) brings in a second.


As for the seemingly irrelevant error message: I guess the agent's state was
a map, so it deref'd it and then invoked the map. Maps can be invoked, but
expect one argument (a key to look up) and it got zero. If the error had
been cannot cast to IFn, it would have been more apparent what was happening
quicker. There isn't much that can be done about the error message confusion
in cases like this, though; it goes with the territory of having a highly
dynamic language. Learning to interpret them is an art, one I'm still
mastering.

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