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 so
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,
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
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 th
> 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 that
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 followi
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
On 8 March 2015 at 20:05, coco 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 passing system, but you
don't e
>
> 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 (
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 cons
>
> 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
( correct
address
res)))
I add this to gist https://gist.github.com/anonymous/9fc79679e17e36c47994
when I try run it I get: CompilerException java.lang.IllegalArgumentException:
Unable to resolve class
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) (+ 2
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
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
yo
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 seem
That works - thanks.
Still, it seems intuitive to build the code as I had originally done.
It's too bad code has to get complicated so quickly :)
Thank again for the help.
On Jan 3, 9:19 pm, Robert Marianski wrote:
> On Tue, Jan 03, 2012 at 07:22:22PM -0800, Trevor wrote:
> >
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 learni
On Jan 3, 7:22 pm, 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.
>
> (defm
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.)
Ah, got it. Your last sentence is very well-put!
--
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
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-
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.
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 wrote:
> Hi,
>
> Not really mechanisms, but two Idioms will help you with your problem:
> Either w
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 [(prims-fro
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
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
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)
>
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.
Can
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)
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 igno
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
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 actua
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 al
One way to think about the difference between alter and commute: With
a commutative function, they both get to the same end result, but
commute allows more concurrency, while guaranteeing less about the
return value. In particular, with commute you might not be able to see
every step by lo
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)))
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)))
> (
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:
>
>>
>> T
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 fun
Excellent, thanks!
Stuart
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
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 (
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 refer
42 matches
Mail list logo