On Thu, Sep 27, 2012 at 8:26 AM, arekanderu <arekand...@gmail.com> wrote:
> Thank you Meikel for your so helpful replies.
>

Thanks also from a lurker, to whom these facts were a useful surprise:
I'd wondered
the same myself.

Cheers, R

>
> On Thursday, September 27, 2012 4:19:44 PM UTC+3, Meikel Brandmeyer
> (kotarak) wrote:
>>
>> 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
>>

-- 
Russell Whitaker
http://twitter.com/OrthoNormalRuss / http://orthonormalruss.blogspot.com/
http://www.linkedin.com/pub/russell-whitaker/0/b86/329

-- 
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

Reply via email to