I concur with Timothy's assessment.  Really well stated and illustrated use
of reduce with a named reduce function.


On Fri, May 3, 2013 at 10:52 PM, Timothy Baldridge <tbaldri...@gmail.com>wrote:

> In general, loop/recur shouldn't be considered idiomatic, IMO. Instead,
> try for a more functional style:
>
>
> due = 100
> cards = cards.map do |card|
>     card.applied_balance = max(0, due - card.balance)
>     due -= card.applied_balance
>
> becomes (untested):
>
> (defn apply-balance-1 [{:keys [due] :as accum} [card-id balance]]
>   (let [applied (max (- due balance))]
>     (-> accum
>          (assoc-in [:applied card-id] applied)
>          (assoc-in [:due] due))))
>
>
> (reduce apply-balance-1
>             {:due 100}
>             {:card-id-1 4404.00
>              :card-id-2 3020.00
>              ....etc....})
>
> Often I have found that using reduce forces me to break functions into
> several parts. If I used loop/recur, normally the function prelude,
> postlude and loop block are all smashed into a single function. With reduce
> + a do-step-1 function (as seen above) we can more easily reason about what
> is happening. The code is then easier to test as well, as we can test the
> calculations apart from the loop logic.
>
> When I'm performing Clojure code reviews, I often consider loop/recur to
> be a code smell.
>
> Timothy
>
>
>
> On Fri, May 3, 2013 at 3:53 PM, Armando Blancas <abm221...@gmail.com>wrote:
>
>> On Friday, May 3, 2013 1:15:24 PM UTC-7, Robert Pitts wrote:
>>>
>>> Armando was a good citizen and sent along a plain-text version as well –
>>> https://groups.google.com/**group/clojure/msg/**
>>> 6aae8287bc55d436?dmode=source&**output=gplain&noredirect<https://groups.google.com/group/clojure/msg/6aae8287bc55d436?dmode=source&output=gplain&noredirect>
>>
>>
>> That must have been Google Groups doing the right thing... nice feature.
>>
>> --
>> --
>> 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
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
>
> --
> “One of the main causes of the fall of the Roman Empire was that–lacking
> zero–they had no way to indicate successful termination of their C
> programs.”
> (Robert Firth)
>
> --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
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
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to