Yes, Kent's solution is spot on! Thank you all for your insights, I
believe we have a winner.
--
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
Ah ok. I couldn't come up with anything, but I think Kent has a nice
eval-free (and macro-free) solution.
My thanks to you, and everyone who chimed in, for helping me better
understand the read-time/compile-time/run-time distinction.
On Sep 11, 8:29 am, icemaze wrote:
> Hi Btsai, thank you for
Actually what you seem to be trying to do is create a new kind
of defn function which creates named functions as a side-effect.
This is similar to the common lisp defstruct function which
creates accessors. Define your own "defn" function, such as
"defthing" and let it do the side-effects for you
The fact that you are trying to generate functions based on data that
is available at run time rather than at compile time is a signal that
you should probably be using functions rather than macros to do what
you want. Here is one way:
(defn make-fn [kwd]
(fn [n] (= n kwd)))
(defn intern-fn [k
> So the problem is solved for me, although I have to use eval. I'm not
> sure exactly how dirty this trick is and especially *why* it is
> considered a "smell". I read it on Paul Graham's "On Lisp" and he
> vehemently opposes its use but he doesn't explain why or where it is
> acceptable. Note tha
Hi Btsai, thank you for your offer for help.
As I said before I *could* use literals but it wouldn't be convenient.
I have a big structure which contains information about "types" (they
are types of domain-specific objects). I would like to extract the
"methods" I need from this structure and defi
I'm sorry, but despite reading through the rest of the thread, it's
not clear to me why that is a problem.
icemaze, could you elaborate on what your use case is? I think with
all of our powers combined, we can come up with something that fits
your needs :)
On Sep 10, 7:24 pm, Robert McIntyre wr
That is very elegant but has the exact same problem in that the macro
must be called on a literal vector of keywords.
--Robert McIntyre
On Fri, Sep 10, 2010 at 5:41 PM, Btsai wrote:
> This is probably not the prettiest way to do it, but I think it gets
> the job done:
>
> (defn make-sym [keyword
This is probably not the prettiest way to do it, but I think it gets
the job done:
(defn make-sym [keyword]
(->> keyword name (str "prefix-") symbol))
(defn make-fn [keyword]
(let [n (gensym)]
(list 'defn (make-sym keyword) [n] (list '= n keyword
(defmacro make-fns [keywords]
`(do ~
You are mostly right in your assumptions: I could dump the keywords in
the clj as literals but it would be tedious and not elegant at all.
Eval's not pretty but it works; plus it's there for a reason, like
working around the shortcomings of the language (and of my brain).
I was about to post my so
Yes, I think I'll have to pass the keywords as literals. I don't think
there's a way around that (other than using eval, as per Alan's
solution).
I was too excited about Hubert's hint and I found myself in the exact
same problem with the second macro.
--
You received this message because you are
there's always apply-macro from contrib for doing perverse stuff like
that, so you don't
have to *see* eval if you don't want to :)
--Robert McIntyre
On Fri, Sep 10, 2010 at 8:24 PM, icemaze wrote:
> I agree: eval never looks pretty.
>
> --
> You received this message because you are subscribed
I agree: eval never looks pretty.
--
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 fr
Yeah, I guess I could use a macro to generate the "call" to the other
macro in a way similar to how you used it. Thank you, that should
definitely work. I'll try it right now.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send
It sounds like he's going to be given the list of keywords by the
user, and won't have it as a compile-time literal. Maybe he's load'ing
a .clj file, for example, that someone will write in the future. Maybe
you can combine our two approaches to get something that works and
isn't ugly, but I'm havi
Why not explicitly feed the macro the keyword map if that is what it wants?
(defmacro ultra-synth [prefix keywords]
(let [symbols (map (comp symbol name) keywords)
fn-names (map (comp symbol
(partial str prefix) name) keywords)
defn-forms (map
PS this is super-ugly and I'm always embarrassed when I find myself
using eval in a lisp. While this works, I'd love it if someone could
tell me how to do it with macros.
On Sep 10, 3:07 pm, Alan wrote:
> I see. It's hard to imagine how this could work, since macros don't
> have access to runtime
I see. It's hard to imagine how this could work, since macros don't
have access to runtime data like the value of x in your example.
Perhaps you're better off writing a function that returns a closure,
and iteratively def'ing those?
user=> (defn make-kw-fn [kw]
#(= kw %))
#'user/make-kw-f
Hi icemaze,
Please look at how fns are generated in http.async.client
http://github.com/neotyk/http.async.client/blob/master/src/http/async/client/util.clj#L54
this macro is later used here:
http://github.com/neotyk/http.async.client/blob/master/src/http/async/client.clj#L43
HTH,
Hubert.
On Se
Alan, thank you for your reply.
Unfortunately your solution is very similar to mine and it suffers
from the same problem (maybe I'm using it incorrectly, I don't know).
If I write:
(doseq [x '(:a :b)]
(make-fn x))
it defines a single function "synthetic-x". Is there a way to make
this work?
SPOILER BELOW. I'm not sure how much help you want, so I went ahead
and wrote your macro. Whitespace padding so that you won't see it if
you don't want the whole solution:
user=> (defmacro make-fn [key]
(let [sym (->> key name (str "synthetic-") symbol)]
`(defn ~sym [n#] (= n# ~key
I actually did this just the other day, to create a simple C-style
enum macro (I assume someone has a better version; this was a learning
exercise). You can see my project at www.github.com/amalloy/enum. It
sounds like your problem might be constructing the symbol to define;
the solution will look
22 matches
Mail list logo