Hi Ed,
I could not reproduce the behavior you described with Leiningen. Here is my
test script:
https://gist.github.com/stuartsierra/ca2b9bd7072224e099beb8b95b49b69c
This issue might be caused by some pre-compiled files on the classpath when
you run `lein test`. Since macros are evaluated durin
Hi,
I've got a problem where specs are not always being checked during macro
expansion. It seems to be reliably be checked when I load the code using
cider (using cider-load-buffer), but not when running lein test at the
terminal.
Code:
(defproject macroexpand-spec-test "0.1.
On similar lines as Ben suggested (but different use case), here's
what I've been doing to rewrite expressions:
(defmacro rewrite-v2
[src & body]
(let [[a x y] (repeatedly gensym)]
`(let [~(with-meta a {:tag "doubles"}) ~src
~x (aget ~a 0)
~y (aget ~a 1)]
~@(po
You also might want to check out this talk http://youtu.be/YHctJMUG8bI
In part of the talk, he describes how they generate symbols deterministically
to use in query fragments that can be predictably combined into Datomic
queries. Different application, but mostly the same requirements as you st
gt;>> you would separate out your templating from your domain functions, separate
>>> out your defmacro from regular functions that just happen to manipulate
>>> symbols. These functions will be easier to test.
>>>
>>> On 23 March 2015 at 16:23, Sean Corfield
t;
>>> On Mar 22, 2015, at 7:52 PM, myguidingstar
>>> wrote:
>>> > I wonder if there is any way to make macro expansion in Clojure
>>> deterministic. That would be useful in unit tests.
>>>
>>> I'd be very interested to understand you
gt; your defmacro from regular functions that just happen to manipulate
> symbols. These functions will be easier to test.
>
> On 23 March 2015 at 16:23, Sean Corfield
> > wrote:
>
>> On Mar 22, 2015, at 7:52 PM, myguidingstar > > wrote:
>> > I
16:23, Sean Corfield wrote:
> On Mar 22, 2015, at 7:52 PM, myguidingstar
> wrote:
> > I wonder if there is any way to make macro expansion in Clojure
> deterministic. That would be useful in unit tests.
>
> I’d be very interested to understand your use case… Testing what the mac
On Mar 22, 2015, at 7:52 PM, myguidingstar
wrote:
> I wonder if there is any way to make macro expansion in Clojure
> deterministic. That would be useful in unit tests.
I’d be very interested to understand your use case… Testing what the macro
expands to seems like it is test the macro
Well, I think it's nondeterministic by design, we usually just test its
behavior, not the form it expanded to.
2015-03-23 10:52 GMT+08:00 myguidingstar :
> Hi all,
> I wonder if there is any way to make macro expansion in Clojure
> deterministic. That would be useful in unit te
Hi all,
I wonder if there is any way to make macro expansion in Clojure
deterministic. That would be useful in unit tests. Something like this:
```
(defmacro lol []
`(let [a# 1] (inc a#)))
(with-predictable-gensym-starting-from-zero
(macroexpand '(lol))
;; => `(let [a0 1]
Doh! Major face palm. I had such a bad case of tunnel vision on the macro
definition I forgot to look up the stack once I'd fixed the macro. Thanks.
Good to have extra eyes.
On Mon, Jul 21, 2014 at 1:04 PM, Matthew DeVore wrote:
> I haven't taken the time to fully grok your macro, but the e
I haven't taken the time to fully grok your macro, but the error is not the
fault of your macro, but of the function invocation. (foo x) is causing the
error because x is undefined. foo is a plain function, not a macro, so it
tries to evaluate each argument. (foo 'x) works fine for me, as does (
In my case, I'm trying to use a the expression '[x] that is available to
the macro in the resulting product. So I don't want the value of x at all,
just a vector with the symbol x.
That vector is in a variable I have in the macro named
'positional-parameters', so I substitute '~positional-paramet
I don't have a Clojure REPL handy at the moment but it looks like the x symbol
is being resolved in the macro context rather than the expansion context. In
the macro source, where you have a plain x, try to replace it with ~'x ... This
blog post may be relevant:
http://amalloy.hubpages.com/hub/
great summary of your experience. I think others will find this helpful.
I was also surprised at first that the metadata is added to the
next form *read* if you use ^ reader macro. This explains the
following behaviour:
(meta ^:matrix 'x) ;=> nil
(meta ' ^:matrix x) ;=> {:matrix true}
in the first
I've summarized a couple of things that wasn't obvious to me when I started
to work on core.matrix's macroses. Comments are appreciated:
http://si14.livejournal.com/51576.html
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group
1) Write itry-fn which takes a function and the source of the
function. Example usage: (itry-fn (fn [] (/ 5 0)) '(/ 5 0))
2) Next write your itry macro to use the function:
(defmacro itry
[expr]
`(itry-fn (fn [] ~expr) '~expr))
A general rule of thumb for macros is that they should provide sug
Hi Andrew,
the approach suggested by Tassilo looks correct.
Macros are evaluated at compile-time, so you need a termination
condition that can be decided without actually running your form. In
you're case you want run-time delayed evaluation, which is easily
achieved by wrapping the expression int
There's no light on in my attic. It's hollow and dark. :-P
(itry (/ 5 0))
==> exception
(itry (fn [] (/ 5 0)))
==> #
But that's ok. I have my ugly macro that uses loop-recur. And I'll be fine.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To po
You should be able to just change your macro to a function, remove the
backtick, and change (try ~expr ...) to (try (eval expr) ...).
- Chris
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.c
Aren't the calls to itry-the-fn different from the calls to itry-the-macro?
For example, let's say my expr is (/ a b) where b is currently zero and
maybe the user decides to set a new value for b when prompted.
itry-the-macro can be called this way: (itry (/ a b)) and is able to print
out the s
Hi Andrew,
your problem is that your recursive macro's termination criterion (no
exception thrown for exp) is only there at runtime, but not at macro
expansion time.
Why don't you use a function here?
(defn itry
"Calls f until it succeeds, querying the user for help."
Well, I found a way to dodge that need. But still interested in whether
it's possible. Here's my dodge. Ugly and proud.
(defmacro itry
"If expr results in an exception, prompt the user to retry or give
up. The user can execute arbitrary code somewhere else or fiddle with
a DB before retrying. R
(defmacro interactive-try
"If expr results in an exception, prompt the user to retry or give
up. The user can execute arbitrary code somewhere else or fiddle with
a DB before retrying. Returns nil if the user gives up."
[expr]
`(try
~expr
(catch Exception e#
(-> (ui/dialog :o
Andrew writes:
> Is there a way for a macro m to expand into code that includes another
> "delayed" use of m such that this second use of m is only expanded if
> needed (and thus avoiding a stack overflow)?
What exactly are you trying to do?
Bye,
Tassilo
--
You received this message because y
Is there a way for a macro m to expand into code that includes another
"delayed" use of m such that this second use of m is only expanded if
needed (and thus avoiding a stack overflow)?
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this
On Mar 17, 2011, at 2:04 PM, Alan wrote:
> On Mar 17, 11:00 am, Meikel Brandmeyer wrote:
>> Hi,
>>
>> Am 17.03.2011 um 18:11 schrieb Alan:
>>
>>> From my uninformed position, strint looks like it should have been
>>> written as a function, not a macro, but probably there are reasons it
>>> was
On Mar 17, 11:00 am, Meikel Brandmeyer wrote:
> Hi,
>
> Am 17.03.2011 um 18:11 schrieb Alan:
>
> > From my uninformed position, strint looks like it should have been
> > written as a function, not a macro, but probably there are reasons it
> > was not.
>
> It can't be „simply“ a function, because
Hi,
Am 17.03.2011 um 18:11 schrieb Alan:
> From my uninformed position, strint looks like it should have been
> written as a function, not a macro, but probably there are reasons it
> was not.
It can't be „simply“ a function, because then it has no access to the local
environment.
(let [x 99]
need tuning and optimization. Can clojureql
help with this?
-Original Message-
From: clojure@googlegroups.com [mailto:clojure@googlegroups.com] On Behalf Of
Alan
Sent: Thursday, March 17, 2011 1:12 PM
To: Clojure
Subject: Re: strint and macro expansion
On Mar 17, 8:34 am, "Bhinderw
On Mar 17, 8:34 am, "Bhinderwala, Shoeb"
wrote:
...use the strint *MACRO*...
...works when I specify my string [as a literal]...
...but doesn't work when I pass the string through a variable.
Macros are not functions. << is receiving as arguments a list with the
two elements 'str and 'q. It must
I have the following definitions and am trying to use the strint macro
(<<) to perform string substitutions.
test1=>(use 'clojure.contrib.strint)
test1=> (def m {:XYZ 1, :ABC 2})
test1=> (def q "select ~(:XYZ m) from ~(:ABC m)")
The following works when I specify my string directly:
test1=> (<<
Thanks for your reply :)
The context: I am developing a wrapper generator that takes a Java
class and generates a Clojure wrapper, with a function for each method
etc. The purpose is to have nice wrappers, so your code is "cleaner"
as with all that Java calls. And it can ba a basis for more conven
Difficult problem.
macro are syntactic tools. So they are not made to evaluate things at runtime.
You could expand to something that call eval at runtime but it is not
a good idea (It involves the compiler at each call)
If your (rest alist) is known at macro-expansion time, then it can
work but
I am a macro newbie... I want to create a macro that calls a function
with a given name and a parameter list on a given object. My first
idea was like this:
(defmacro call
"Calls an instance method on a given object with a list of params."
[obj method-name params]
`(. ~obj ~(symbol method-na
No problem Evan,
(def constants (atom ()))
(defn add-constant [name x]
(let [sym (gensym name)]
(reset! constants (cons ([sym x]) @constants)
sym
))
Then when I produce code, I call add-constant and use the returned symbol.
(I only use it to produce new constants, which is the ann
Hi Nicolas,
> What I do currently:
> I keep all the constant definitions and the rules definitions in an
> atom and at the end, I call a macro at top level that define
> everything, in the dependency order.
Would you mind posting an example of this? That would definitely help
me to better underst
Dear all,
In my project, I need (in order to share constant trees in rule
definitions), to define new variable during macro expansion.
What I do currently:
I keep all the constant definitions and the rules definitions in an
atom and at the end, I call a macro at top level that define
everything
specifically about exceptions that occur during macro
> expansion. As often as not, these exceptions are the result of calling the
> macro with incorrect arguments rather than any problem in the macro itself,
> but the stack trace doesn't contain enough information to locate the
&
I'm concerned specifically about exceptions that occur during macro
expansion. As often as not, these exceptions are the result of calling the
macro with incorrect arguments rather than any problem in the macro itself,
but the stack trace doesn't contain enough information to locate the
+1 on this. Although of course you could just use the shell for this
(e.g. grep or awk). But it's certainly nicer to have that integrated
in the compiler (possibly also in the compiled code?)
On Feb 9, 12:45 pm, Jeff Rose wrote:
> I agree, the error reporting from the compiler can often be hard t
I agree, the error reporting from the compiler can often be hard to
dig through. Besides showing both the location of the macro
definition and its usage, it would be nice to hide all of the
clojure.lang.* calls in the stack trace by default, or fold them into
a single line. That way the user code
se the (/ 0 0)
bit gets executed at macro expansion time. You'd have to syntax-quote
it to fail at runtime:
(defmacro broken [] `(/ 0 0))
(A regular quote would probably also do.)
Also, note the user$broken ... line in your stack trace -- it does
contain a useful indication of the source of the
The Clojure compiler is not very helpful when it comes to debugging
exceptions that occur while macros are being expanded. As an example,
consider this code:
;; macro-fail.clj
(defmacro broken [] (/ 0 0))
(broken)
Here's the stack trace I get when I compile this file:
Exception in thread "main"
On 21.04.2009, at 00:26, Mark Volkmann wrote:
> Is it correct to say that macros are expanded at read-time?
No. Only reader macros are expanded at read time, standard macros are
expanded at compile time.
A more subtle point concerns the distinction between compile time and
run time, because
But "reader macros" *are* expanded at read-time (as their name
indicates, BTW) :
1:91 user=> (read-string "'quoted-symbol")
(quote quoted-symbol)
1:92 user=>
HTH,
--
Laurent
2009/4/21 Laurent PETIT :
> 2009/4/21 Mark Volkmann :
>>
>> I didn't explain my question well enough. Suppose I define
2009/4/21 Mark Volkmann :
>
> I didn't explain my question well enough. Suppose I define a macro
> with defmacro and have several calls to it in my code. When are those
> calls expanded to the code inside the macro? Is that at read-time?
Maybe the example I gave (by using the macro 'defn) was not
Hi Mark,
Am 21.04.2009 um 00:40 schrieb Mark Volkmann:
I didn't explain my question well enough. Suppose I define a macro
with defmacro and have several calls to it in my code. When are those
calls expanded to the code inside the macro? Is that at read-time?
As Laurent said: No.
I didn't explain my question well enough. Suppose I define a macro
with defmacro and have several calls to it in my code. When are those
calls expanded to the code inside the macro? Is that at read-time?
On Mon, Apr 20, 2009 at 5:34 PM, Laurent PETIT wrote:
>
> Hello,
>
> Apparently, no:
>
> 1:8
Hello,
Apparently, no:
1:85 user=> (macroexpand-1 '(defn hello [] "world"))
(def hello (clojure.core/fn ([] "world")))
1:86 user=> (read-string "(defn hello [] \"world\")")
(defn hello [] "world")
1:87 user=>
read-string did not expand defn.
I think it's 'eval that expands macros and compiles
In my Clojure article at http://ociweb.com/mark/clojure/article.html I say:
"Clojure code is processed in three phases: read-time, compile-time
and run-time. At read-time the Reader reads source code and converts
it to a data structure, mostly a list of lists of lists At
compile-time this da
press-namespaces* true]
>> >> (with-pprint-dispatch *code-dispatch*
>> >> (write (expander (read-from-string string)) :pretty true :stream
>> >> nil
>>
>> >> Now that pprint is a part of contrib, these two changes should probably be
pander (read-from-string string)) :pretty true :stream
> >> nil
>
> >> Now that pprint is a part of contrib, these two changes should probably be
> >> incorporated into swank-clojure.
>
> >> On Fri, Apr 17, 2009 at 2:39 PM, Paul Drummond
> >&g
g)) :pretty true :stream
> >> nil
> >>
> >> Now that pprint is a part of contrib, these two changes should probably
> be
> >> incorporated into swank-clojure.
> >>
> >> On Fri, Apr 17, 2009 at 2:39 PM, Paul Drummond <
> paul.drumm...@io
> incorporated into swank-clojure.
>>
>> On Fri, Apr 17, 2009 at 2:39 PM, Paul Drummond
>> wrote:
>>
>>
>>
>> > Up until now I have made do with a simple clojure repl in Emacs but as
>> > I am playing around with macros I feel it's time to
two changes should probably be
> incorporated into swank-clojure.
>
> On Fri, Apr 17, 2009 at 2:39 PM, Paul Drummond
> wrote:
>
>
>
> > Up until now I have made do with a simple clojure repl in Emacs but as
> > I am playing around with macros I feel it's time
probably be
incorporated into swank-clojure.
On Fri, Apr 17, 2009 at 2:39 PM, Paul Drummond wrote:
>
> Up until now I have made do with a simple clojure repl in Emacs but as
> I am playing around with macros I feel it's time to test drive
> clojure-swank and it's fancy ma
Up until now I have made do with a simple clojure repl in Emacs but as
I am playing around with macros I feel it's time to test drive
clojure-swank and it's fancy macro expansion features!
I have installed clojure-swank following the instructions on the wiki
and the installation see
d (= a b) (= c d)) true (cond (or (= e f) (= g h)) false))
The initial 'cond' has been expanded into an 'if', but cond is a
recursive macro and the expansion produces another 'cond'. Furthermore
the 'and' & 'or' macros are unexpanded.
So, I wro
60 matches
Mail list logo