Re: [OT] Re: More idiomatic way to use map like this?

2013-05-04 Thread Cedric Greevey
What about the times when one simply must use loop/recur for performance
reasons? Although, one thought I had on that was to write a functional
version, and if it's a performance bottleneck, write a loop/recur version
and call the latter in performance-critical areas, but also have tests that
check that both versions have the same semantics.


On Sat, May 4, 2013 at 1:54 AM, Alex Baranosky 
alexander.barano...@gmail.com wrote:

 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.comwrote:

 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.comwrote:

 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=gplainnoredirecthttps://groups.google.com/group/clojure/msg/6aae8287bc55d436?dmode=sourceoutput=gplainnoredirect


 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.




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

Re: [OT] Re: More idiomatic way to use map like this?

2013-05-04 Thread Devin Walters
I don't think that it's productive to discuss idiomatic code and performance in 
the same breath.

People do all sorts of nasty stuff when trying to squeeze performance juice out 
of their code. In my experience it's rare to see performance-related idioms 
beyond the obvious language-level constructs like type hints and indeed 
loop/recur. When serious performance concerns come into play, idioms are often 
broken via the use of clever tricks.

In the case of loop/recur I think I agree with Tim, Alex, and company.

What you're bringing up is a special case, so to your question I would respond 
with a question: What makes a context-specific performance-related concern 
idiomatic? My guess is very little.

In summary I would say that loop/recur is idiomatic when there is a serious, 
verifiable performance concern, but the overarching idiom is to prefer reduce 
as it's more consistent with Clojure's philosophy and design. Loop/recur is 
kind of like swearing. Reduce is typical conversation.  

2c,
--  
{:∂evin :√valters}


On Saturday, May 4, 2013 at 2:43 PM, Cedric Greevey wrote:

 What about the times when one simply must use loop/recur for performance 
 reasons? Although, one thought I had on that was to write a functional 
 version, and if it's a performance bottleneck, write a loop/recur version and 
 call the latter in performance-critical areas, but also have tests that check 
 that both versions have the same semantics.
  
  
 On Sat, May 4, 2013 at 1:54 AM, Alex Baranosky alexander.barano...@gmail.com 
 (mailto:alexander.barano...@gmail.com) wrote:
  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 
  (mailto: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 
   (mailto: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=sourceoutput=gplainnoredirect
 
 
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 
(mailto: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 
(mailto:clojure%2bunsubscr...@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 
(mailto:clojure%2bunsubscr...@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 
   (mailto:clojure@googlegroups.com)
   Note that posts from new members are moderated - please be patient with 
   your first post.
   To 

[OT] Re: More idiomatic way to use map like this?

2013-05-03 Thread Cedric Greevey
On Fri, May 3, 2013 at 2:32 PM, Alan Thompson thompson2...@gmail.comwrote:

 Hey Armando - How did you get the nice syntax highlighting into your
 post??? Enquiring minds wanna know.
 Alan


If the OP's problem is solved anyway, I suppose we can go OT a bit with
this thread now.

AFAIK, it's done by sending the list an HTML email. I'm not sure what tools
are used to generate the formatting, though, or of the netiquette of
posting in HTML. Those posts render well in the Groups interface and in
gmail, but for anyone using other mail clients to use the list, their
mileage will vary. It probably looks like a horrendous mess to anyone using
mutt, elm, or XFMail, for example. Are the majority of readers here using
gmail or Groups, then?

(On a related note, there are a lot of top-posted replies and I'm probably
guilty of a few of them myself. The gmail web interface makes it
significantly more effort to bottom-post, whereas Google Groups makes it
more effort to top-post, last time I checked out its interface. I haven't
seen complaints here about either type of post, though, or about HTML mail
to the list, so it seems as if all three are generally accepted here.)

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




Re: [OT] Re: More idiomatic way to use map like this?

2013-05-03 Thread Robert Pitts
Armando was a good citizen and sent along a plain-text version as well 
– 
https://groups.google.com/group/clojure/msg/6aae8287bc55d436?dmode=sourceoutput=gplainnoredirect

Would still be nifty to know if there's an easy way to do this, Armando :)

On Friday, May 3, 2013 3:19:48 PM UTC-4, Cedric Greevey wrote:

 On Fri, May 3, 2013 at 2:32 PM, Alan Thompson 
 thomps...@gmail.comjavascript:
  wrote:

 Hey Armando - How did you get the nice syntax highlighting into your
 post??? Enquiring minds wanna know.
 Alan


 If the OP's problem is solved anyway, I suppose we can go OT a bit with 
 this thread now.

 AFAIK, it's done by sending the list an HTML email. I'm not sure what 
 tools are used to generate the formatting, though, or of the netiquette of 
 posting in HTML. Those posts render well in the Groups interface and in 
 gmail, but for anyone using other mail clients to use the list, their 
 mileage will vary. It probably looks like a horrendous mess to anyone using 
 mutt, elm, or XFMail, for example. Are the majority of readers here using 
 gmail or Groups, then?

 (On a related note, there are a lot of top-posted replies and I'm probably 
 guilty of a few of them myself. The gmail web interface makes it 
 significantly more effort to bottom-post, whereas Google Groups makes it 
 more effort to top-post, last time I checked out its interface. I haven't 
 seen complaints here about either type of post, though, or about HTML mail 
 to the list, so it seems as if all three are generally accepted here.)



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




Re: [OT] Re: More idiomatic way to use map like this?

2013-05-03 Thread Armando Blancas
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=sourceoutput=gplainnoredirect


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.




Re: [OT] Re: More idiomatic way to use map like this?

2013-05-03 Thread Timothy Baldridge
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=gplainnoredirecthttps://groups.google.com/group/clojure/msg/6aae8287bc55d436?dmode=sourceoutput=gplainnoredirect


 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.




Re: [OT] Re: More idiomatic way to use map like this?

2013-05-03 Thread Alex Baranosky
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.comwrote:

 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.comwrote:

 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=gplainnoredirecthttps://groups.google.com/group/clojure/msg/6aae8287bc55d436?dmode=sourceoutput=gplainnoredirect


 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.