Hi, Am Donnerstag, 27. September 2012 12:16:41 UTC+2 schrieb arekanderu:
I am new to clojure and I have two questions about do and the way it should > be used. > > *Question 1: Which of the following two functions is more idiomatic and > why? Both functions produce the same result.* > > <code> > (defn my-fn [java-object] > (. java-object firstFunc) > (. java-object secondFunc) > (. java-object thirdFunc) > java-object) > </code> > The first because defn includes an implicit do. So the second example is actually (do (do ...)). In this case you could also use doto: (defn my-fn [pojo] (doto pojo .firstFunc .secondFunc .thirdFunc)) > *Question 2: Again, which one is more idiomatic and why? Both functions > produce the same result.* > * > * > <code> > (defn my-fn [java-object bar] > (let [bar-bar (. java-object getSomething) > _ (if (not (is-bar? bar)) > (. java-object (setSomething bar-bar)))] > java-object)) > </code> > > <code> > (defn my-fn [java-object bar] > (let [bar-bar (. java-object getSomething)] > (do > (if (not (is-bar? bar)) > (. java-object (setSomething bar-bar))) > java-object))) > </code> > The third: (defn my-fn [pojo bar] (let [bar-bar (.getSomething pojo)] (when-not (is-bar? bar) (.setSomething pojo bar-bar)) pojo))) let also (just like defn) includes an implicit do for the body. Hope this helps. Kind regards Meikel -- 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