Great! Thanks all for your explanations.
On Wednesday, January 27, 2016 at 4:33:40 AM UTC-8, gianluca torta wrote:
>
> Hi Dan,
>
>
> And, if I understand correctly, what was really happening with the macro
>> bar was that in ('+ 2 'u), '+ was looking itself up in 2, not finding
>> itself, and
Hi Dan,
And, if I understand correctly, what was really happening with the macro
> bar was that in ('+ 2 'u), '+ was looking itself up in 2, not finding
> itself, and so returning the default value 'u. This was the expansion of
> the macro, and so at run time, it was bound to 10.
>
>
exactly,
I'm new to clojure and macros and am having trouble wrapping my head around
some simple things.
I know that this macro will add 2 to its argument:
user=> (defmacro plus-two [x] `(+ 2 ~x))
#'user/plus-two
user=> (plus-two 5)
7
user=> (def u 10)
#'user/u
user=> (plus-two u)
12
I tried the
> I know that this macro will add 2 to its argument:
This isn't what that macro does. That macro takes a piece of syntax, and
sticks that syntax in the hole in the expression (+ 2 HOLE). The
macro doesn't evaluate the expression, it just generates it. It is the
evaluation of *that* expression
Thanks for your reply. I guess I was forgetting that at macroexpansion time
(if I'm using the word correctly), we don't have access to the binding
between u and 10. The same error I message I noted above does appear for (+
2 'u).
And, if I understand correctly, what was really happening with
A macro emits a form for the compiler to compile. And it understands its
inputs as forms -- unevaluated forms.
- Your plus-two emits a list with + in function position.
- foo, when it is computing the form to emit, applies + to 2 and some form
x. So 5 works but u does not, even after (def u
All your macro needs to do is to convert the first form into the second.
thanks james, that make sense for me...
I don't know what the rest of your function is supposed to be doing
you can check my second example
(def data (atom ))
(defmacro blahhh []
(go
(let [ch
Sure, because you haven't quoted the symbol. If you write:
(str *ns* : some-bus)
ups!!, yes I forget quoted the symbol, thanks james!!...
In Clojure, then Clojure will look for a variable called some-bus. The
error you get is because it can't find such a variable.
You want to
On 8 March 2015 at 20:05, coco clasesparticulares...@gmail.com wrote:
I don't know what the rest of your function is supposed to be doing
you can check my second example
I'm afraid that doesn't explain you intend your function to do.
You appear to be setting up some form of message
You're not using macros correctly.
A macro does not evaluate its arguments, but it does evaluate its return
value. The only thing a macro should do is to transform one piece of code
into another.
So let's look at what you want your syntax to look like:
(send! (function-blah hi!))
Now
sorry for my crappy code, but I was changing some conditions and testing my
parameters before and I end with this code
(if (not (false? res))
res
(recur))
I must write just
(if res
res
(recur))
--
You received this message because you are subscribed to the Google
Hi guys, first I'm really noob using macros, basically I've this
(!b (function-blah hi))
Hey..I didn't know than intellij copy/paste the text with format!..cool!
Ok..returning to my question...I don't need call to function-blah...I need
send a message to one address possibly named
you can also use macroexpand-1 to see how your macro expands
for example, having defined your macro as suggested by Gary and James
above, you can write something like:
(macroexpand-1 '(if-zero (- 4 2 2) (+ 1 1) (+ 2 2)))
and see that it correctly to:
(if (clojure.core/= (- 4 2 2) 0) (+ 1 1) (+
Disclosure: I'm rather fascinated with macros (the things they are
supposed to allow us to do), but I'm still rather mystified about how
they actually work. It seems like every time I try to write one, it
rarely behaves anything like I expect.
Question: Is there any obvious problems with this
Yes, you need to revisit the fundamentals of macros.
Macros are functions that take in data (the forms), and return data
(possibly altered forms).
In your example, you're checking if the expression itself, a list or some
kind of value or something is equal to the value 0, at compile-time, then
Macros differ from functions in two ways:
1. They're executed at compile-time, rather than run-time.
2. Their arguments are unevaluated.
Your if-zero function probably doesn't act the way you expect it to,
because you're expecting test-expr to be evaluated.
If all of the arguments to your macro
On Tue, Jan 03, 2012 at 07:22:22PM -0800, Trevor wrote:
hmmm macro question:
; here's a macro that assembles many puts. It serves no purpose to me
(and doesn't even make sense in its current form). It's just something
I hit playing around and learning macros.
(defmacro doto-putter [x
macro question:
; here's a macro that assembles many puts. It serves no purpose to me
(and doesn't even make sense in its current form). It's just something
I hit playing around and learning macros.
(defmacro doto-putter [x y xs]
`(doto (java.util.HashMap.)
(.put ~x ~y
hmmm macro question:
; here's a macro that assembles many puts. It serves no purpose to me
(and doesn't even make sense in its current form). It's just something
I hit playing around and learning macros.
(defmacro doto-putter [x y xs]
`(doto (java.util.HashMap.)
(.put ~x ~y
On Jan 3, 7:22 pm, Trevor tcr1...@gmail.com wrote:
hmmm macro question:
; here's a macro that assembles many puts. It serves no purpose to me
(and doesn't even make sense in its current form). It's just something
I hit playing around and learning macros.
(defmacro doto-putter [x y xs
Hi everyone,
I'm working my way through Practical Clojure's macro chapter, and I'd like
to check my understanding about one of the examples.
The book discusses writing a macro to randomly evaluate a form out of a list
of forms--essentially a cond that randomly selects which branch to evaluate.
Yes, definitely. Your version selects which form to evaluated once, at
compile time; the original selects a form every time the expansion is
evaluated.
user (defn rand-num [] (rand-expr-mulit 1 2 3 4 5 6 7 8 9))
#'user/rand-num
user (rand-num)
7
user (rand-num)
7
user (rand-num)
7
user (rand-num)
Hi all,
I'm running into a problem where I need to define several functions
and/or vars inside another function, but the function has multiple
definitions and they all need to have access to the inner things.
This could be solved by making private functions and using partial,
but this sounds like
Hi,
Not really mechanisms, but two Idioms will help you with your problem:
Either wrap the defn in a let:
(let [U (gen-matrix [1 2 2 -2 -1 -2 2 2 3] 3 3)
A (gen-matrix [1 2 2 2 1 2 2 2 3] 3 3)
D (gen-matrix [-1 -2 -2 2 1 2 2 2 3] 3 3)
S (gen-vector [3 4 5])]
(letfn
The first solution looks good. I had no idea that wrapping a defn
inside of let's would leave the function exposed. Interesting. Is it
considered idiomatic though?
On May 30, 2:16 pm, Erik Söhnel eriksoeh...@googlemail.com wrote:
Hi,
Not really mechanisms, but two Idioms will help you with
Hello and happy new year,
I've started this year with playing around with clojure macros and
wrote a macro that
behaves in a way I don't understand:
(defmacro foo
([x] `(list ~x ~x))
([x n] (if (= n 0)
`(foo ~x)
`(foo ~(foo x)
~(- n 1)
(foo :a 0)
Hello,
just a follow up: I discovered that I sent the macro def twice and
than applied the macro. If the first time the defmacro is evaluated
then the resulting macro works as I expected. But when I send the same
defmacro a second time to the interpreter, the macro behaves as
described below.
On Jan 1, 2009, at 5:45 AM, synphonix wrote:
Hello and happy new year,
I've started this year with playing around with clojure macros and
wrote a macro that
behaves in a way I don't understand:
(defmacro foo
([x] `(list ~x ~x))
([x n] (if (= n 0)
`(foo ~x)
Thanks a lot.
This year starts well (I learned something :-)
Regards
Poul
--~--~-~--~~~---~--~~
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
To unsubscribe
Hi,
Am 09.11.2008 um 12:23 schrieb Stuart Halloway:
Just to make things even more fun: *None* of the proposed fixes to the
concurrency bug in the original actually preserve the` semantics of
the original. All have moved from run (usually) once, mark as done
to mark as done, try once. This also
You should be able to do this without the ref. Have the agent's state
contain a pair of [has-run, fn-result].
Stuart
Hi,
Am 09.11.2008 um 12:23 schrieb Stuart Halloway:
Just to make things even more fun: *None* of the proposed fixes to
the
concurrency bug in the original actually
On Nov 9, 8:21 am, Stuart Halloway [EMAIL PROTECTED] wrote:
You should be able to do this without the ref. Have the agent's state
contain a pair of [has-run, fn-result].
The semantics of your runonce aren't clear to me, but here are some
strategies:
As Chouser proposed, if you only want a
Hi Rich,
Wow, that's a comprehensive answer. Thanks.
I am using runonce to create lancet, a build system that ties into
Java's Ant. Build steps definitely do have side effects, so I will
probably end up using the agent approach. Targets don't have (or
ignore) return values, so I could
The defrunonce macro below works, creating a function that runs only
once and tracking its run status in metadata.
Now, how do I write it without using eval?
(defn runonce
Create a function that will only run once, given a function. Returns
a vector containing the function and the
On Sat, Nov 8, 2008 at 10:23 PM, Stuart Halloway
[EMAIL PROTECTED] wrote:
How about:
(defn runonce
Create a function that will only run its argument once.
[function]
(let [call-count (ref 0)]
(fn [ args]
(when (= 1 (dosync (alter call-count inc)))
(apply function
On Nov 8, 2008, at 10:23 PM, Stuart Halloway wrote:
How about:
(defn runonce
Create a function that will only run its argument once.
[function]
(let [call-count (ref 0)]
(fn [ args]
(when (= 1 (dosync (alter call-count inc)))
(apply function args)
The
How about:
(defn runonce
Create a function that will only run its argument once.
[function]
(let [call-count (ref 0)]
(fn [ args]
(when (= 1 (dosync (alter call-count inc)))
(apply function args)
On Nov 8, 2008, at 8:31 PM, Stuart Halloway wrote:
The
On Sat, Nov 8, 2008 at 8:31 PM, Stuart Halloway
[EMAIL PROTECTED] wrote:
(defmacro defrunonce [sym doc forms]
Defines a function with runonce semantics. Curren run status
is kept in a reference under the :has-run metadata key.
(let [[function has-run] (runonce (eval (concat (list 'fn
On Nov 8, 2008, at 8:31 PM, Stuart Halloway wrote:
The defrunonce macro below works, creating a function that runs only
once and tracking its run status in metadata.
Now, how do I write it without using eval?
(defn runonce
Create a function that will only run once, given a function.
39 matches
Mail list logo