RE: Translating Java code with nested for loops

2011-06-29 Thread Bhinderwala, Shoeb
- From: clojure@googlegroups.com [mailto:clojure@googlegroups.com] On Behalf Of Huahai Yang Sent: Thursday, June 30, 2011 12:33 AM To: Clojure Subject: Re: Translating Java code with nested for loops Missed that one. Yeah, that's much nicer. -huahai On Jun 29, 6:39 pm, Alan Malloy

Re: Translating Java code with nested for loops

2011-06-29 Thread Huahai Yang
Missed that one. Yeah, that's much nicer. -huahai On Jun 29, 6:39 pm, Alan Malloy wrote: > (* daily (reduce * 1 coll)) > ;; is exactly equal to > (reduce * daily coll) > > My point is you can start with daily instead of with 1, and thus not > have to special-case it in at the end. > > On Jun 29,

Re: Translating Java code with nested for loops

2011-06-29 Thread Alan Malloy
(* daily (reduce * 1 coll)) ;; is exactly equal to (reduce * daily coll) My point is you can start with daily instead of with 1, and thus not have to special-case it in at the end. On Jun 29, 8:22 am, Huahai Yang wrote: > The multiplication outside the reduce is required because it is part > of

RE: Translating Java code with nested for loops

2011-06-29 Thread Bhinderwala, Shoeb
. -Original Message- From: clojure@googlegroups.com [mailto:clojure@googlegroups.com] On Behalf Of Alan Malloy Sent: Wednesday, June 29, 2011 4:50 AM To: Clojure Subject: Re: Translating Java code with nested for loops On Jun 29, 12:55 am, Ken Wesson wrote: > On Wed, Jun 29, 2011 at 3:53

Re: Translating Java code with nested for loops

2011-06-29 Thread Huahai Yang
The multiplication outside the reduce is required because it is part of the original logic: "contrib[i] = sum * dailyValues[i];". Using the drop function is a very good call though. -huahai On Jun 29, 12:53 am, Alan Malloy wrote: > If you're already doing a bunch of multiplication in the reduce,

Re: Translating Java code with nested for loops

2011-06-29 Thread Alan Malloy
On Jun 29, 12:55 am, Ken Wesson wrote: > On Wed, Jun 29, 2011 at 3:53 AM, Alan Malloy wrote: > > This layout makes it fairly clear that the first element of total- > > values is never being used, but it would be nice if we could say so > > explicitly. So finally, we can wrap the whole thing in a

Re: Translating Java code with nested for loops

2011-06-29 Thread Ken Wesson
On Wed, Jun 29, 2011 at 3:53 AM, Alan Malloy wrote: > This layout makes it fairly clear that the first element of total- > values is never being used, but it would be nice if we could say so > explicitly. So finally, we can wrap the whole thing in a let: > > (defn calc [total-values daily-values]

Re: Translating Java code with nested for loops

2011-06-29 Thread Alan Malloy
If you're already doing a bunch of multiplication in the reduce, you don't need to do it outside as well, do you? And because reduce doesn't care about laziness, you can use drop instead of nthnext (which I think is clearer in almost all cases). (defn calc [total-values daily-values] (map-index

Re: Translating Java code with nested for loops

2011-06-28 Thread hci
How about this one? (defn calc [total-values daily-values] (map-indexed (fn [i daily] (* daily (reduce #(* %1 (inc (/ %2 100.0))) 1.0 (nthnext total- values (inc i) daily-values)) Happy coding. -huahai On Jun 28, 10:11 pm, Justin Kramer wrote: > Here's one wa

Re: Translating Java code with nested for loops

2011-06-28 Thread David Sletten
On Jun 29, 2011, at 1:45 AM, David Sletten wrote: > > (defn compute-contrib [daily-values total-values] > (loop [contrib [] > daily-values daily-values > total-values total-values] > (if (empty? daily-values) > contrib > (recur (conj contrib (* (first daily-val

Re: Translating Java code with nested for loops

2011-06-28 Thread David Sletten
On Jun 28, 2011, at 11:37 PM, David Sletten wrote: > > On Jun 28, 2011, at 8:20 PM, Bhinderwala, Shoeb wrote: >> The inputs are two arrays of type double of the same length – dailyValues >> and totalValues. The output is the array contrib of the same length. >> >> >> int n = dailyValu

Re: Translating Java code with nested for loops

2011-06-28 Thread Justin Kramer
Here's one way. (defn tails [coll] (take-while seq (iterate rest coll))) (defn calc [total-values daily-values] (map * daily-values (for [tail (tails total-values)] (reduce #(* %1 (inc (/ %2 100.0))) 1.0 (

Re: Translating Java code with nested for loops

2011-06-28 Thread David Sletten
On Jun 28, 2011, at 8:20 PM, Bhinderwala, Shoeb wrote: > The inputs are two arrays of type double of the same length – dailyValues and > totalValues. The output is the array contrib of the same length. > > > int n = dailyValues.length; > > for (int i = 0; i < n; i++) > >