Re: Let over defmacro

2011-05-03 Thread Ken Wesson
You had:

(defmacro eval-cached [obj]
  (when-not (contains? @cache obj)
(reset! cache (assoc @cache obj (eval obj
  (@cache obj))

and:

(eval-cached fbody)

This results in eval-cached being called with obj bound to the symbol
'fbody rather than to the second argument to def-cached -- probably
not what you intended. It checks the cache for containing a value at
the key 'fbody, fails, and so goes to create the entry -- and calls
(eval 'fbody), rather than (eval the-code-you-passed-to-def-cached).

You really did need a function and not a macro for eval-cached.

-- 
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: Let over defmacro

2011-05-03 Thread Stanislav Paskalev
Hello again,

This has solved the issue:

(let [cache (atom {})
  cache-eval (fn [obj]
(when-not (contains? @cache obj)
  (reset! cache (assoc @cache obj (eval obj
(@cache obj))]
  (defmacro def-cached
"Just as def, however it will return an existing object if it has
already been defined with it"
[fname fbody]
(intern *ns* (symbol fname) (cache-eval fbody)))
  (defmacro eval-cached
"Just as eval, however it will return an existing object if it has
already been evaluated with it"
[obj]
(cache-eval obj)))

However it'll be good if someone could share some insight why a macro
cannot call another one and what is the general solution in such a
case

Stanislav Paskalev



On Tue, May 3, 2011 at 3:50 PM, Stanislav Paskalev  wrote:
> Hello,
> Evaluating the form
>
> (let [cache (atom {})]
>  (defmacro eval-cached
>        "Just as eval, however it will return an existing object if it has
> already been evaluated with it"
>        [obj]
>        (when-not (contains? @cache obj)
>          (reset! cache (assoc @cache obj (eval obj
>        (@cache obj))
>  (defmacro def-cached
>        "Just as def, however it will return an existing object if it has
> already been defined with it"
>        [fname fbody]
>        (intern *ns* (symbol fname) (eval-cached fbody
>
> Gives me a java.lang.UnsupportedOperationException: Can't eval locals
> - for the last line of "def-cached"
>
> However, when I use eval the first defmacro by itself as a closure -
> it works perfectly. How can I make those share the cache state without
> binding it to a public symbol ?
>
> Best regards,
> Stanislav Paskalev
>

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


Let over defmacro

2011-05-03 Thread Stanislav Paskalev
Hello,
Evaluating the form

(let [cache (atom {})]
  (defmacro eval-cached
"Just as eval, however it will return an existing object if it has
already been evaluated with it"
[obj]
(when-not (contains? @cache obj)
  (reset! cache (assoc @cache obj (eval obj
(@cache obj))
  (defmacro def-cached
"Just as def, however it will return an existing object if it has
already been defined with it"
[fname fbody]
(intern *ns* (symbol fname) (eval-cached fbody

Gives me a java.lang.UnsupportedOperationException: Can't eval locals
- for the last line of "def-cached"

However, when I use eval the first defmacro by itself as a closure -
it works perfectly. How can I make those share the cache state without
binding it to a public symbol ?

Best regards,
Stanislav Paskalev

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