I'm confused. How does the following not work? (let [a 1 b 2 c 3] (println a) (println b) (println c) (+ a b c))
On Thursday, October 18, 2012 5:01:33 PM UTC+1, JvJ wrote: > > I'm not sure if anyone's done this before, but I'm fed up with writing > code that looks like this: > > (let [a 1] > (println "this is a: " a) > (let [b 2] > (println "this is b: " b) > (let [c 3] > (println "this is c: " c) > (+ a b c)))) > > I'd rather do something more like this: > (-:> > (:= a 1) ;; Assign a to 1 > (println "this is a: " a) > (:= b 2) > (println "this is b: " b) > (:= c 3) > (println "this is c: " c) > (+ a b c)) > > > I wrote a macro to do this, but I feel like it's such a simple thing that > someone must have done it before, and it's probably better than mine. > Does anyone know of anything like that? > > Here's the code for it in case anyone wants to use it: > > (ns cljutils.core) > > (defn- form-check > "Ensures the form represents an assignment. > Such as (:= a 1)" > [form] > (and > (= 3 (count form)) > (= := (first form)) > (symbol? (second form)))) > > > (defn- iblk-fn > "Simulates imperative variable-assignments through nested > let statements." > [& forms] > (let [form (first forms) > rforms (rest forms)] > (if-not form > nil > ;; Else > `(do > ~@(if (form-check form) > `((let [~(second form) ~(nth form 2)] > ~(apply iblk-fn rforms))) > (let [[f r] (split-with (comp not form-check) forms)] > (concat > f > (apply iblk-fn r)))))))) > > (defmacro -:> > [& forms] > (apply iblk-fn forms)) > -- 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 from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en