Hi, Am 12.02.2009 um 16:15 schrieb Mark Fredrickson:
I dislike this strategy for two related reasons: (1) It cannont be nested. Here is a pseudo-example: (?-> 2 ... some computations ... (f (?-> ... I don't have access to the outer "?" anymore... ))) (2) Shadowing user bindings. If I bind ? to something in my code (e.g. (let [? (pred? foo)] (....))) the ?-> form shadows my definition, preventing access.
These are valid points. And I think you are right in that (in case there is an easy solution) there shouldn't be arbitrary restrictions.
Thoughts?
I wouldn't make things to complicated. I think that eg. -> can be well handled with this same macro by simple specifying a local at the right place. However I would not name it let-> since there is no binding vector following. I would suggest pipe-as. This also explains, that the given local is bound to different things during the different stages of the pipe. What I definitively want is the non-seq => (non-seq local) translation. That's really nice. For the function vs. let discussion: I use anonymous functions a lot in macros to package things up in a closure and then expand the macro to some code calling a driver function passing said anonymous function at runtime. However, in this case it is not really needed. Let is sufficient. And since each function generates a new classfile, while this does not happen for let, I would stick with the latter. user=> (defmacro pipe-as ([_ expr] expr) ([local expr form & more] `(let [~local ~expr] (pipe-as ~local ~(if (seq? form) form (list form local)) ~...@more)))) #'user/pipe-as user=> (pipe-as x (+ 1 2) (* 3 x) inc) 10
(mullti-macros anyone?).
user=> (defmulti foo* (fn [x a b] x)) #'user/foo* user=> (defmethod foo* 'add [_ a b] `(+ ~a ~b)) #<MultiFn clojure.lang.mult...@e6f711> user=> (defmethod foo* 'sub [_ a b] `(- ~a ~b)) #<MultiFn clojure.lang.mult...@e6f711> user=> (defmacro foo [x a b] (foo* x a b)) #'user/foo user=> (macroexpand-1 '(foo sub 1 2)) (clojure.core/- 1 2) user=> (macroexpand-1 '(foo add 1 2)) (clojure.core/+ 1 2) ;) Sincerely Meikel
smime.p7s
Description: S/MIME cryptographic signature