Re: Translating Java code with nested for loops

2011-06-29 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-values)

Aw: Translating Java code with nested for loops

2011-06-29 Thread Meikel Brandmeyer
Hi, just for fun another variation. The two reverse calls are ugly, but necessary to get the order right for the reductions call. (defn contribution [daily-values total-values] (let [weights (- (rest total-values) (map #(/ % 100.0)) reverse

Re: Translating Java code with nested for loops

2011-06-29 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 jkkra...@gmail.com

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]

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 a...@malloys.org 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

Re: Translating Java code with nested for loops

2011-06-29 Thread Alan Malloy
On Jun 29, 12:55 am, Ken Wesson kwess...@gmail.com wrote: On Wed, Jun 29, 2011 at 3:53 AM, Alan Malloy a...@malloys.org 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

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 a...@malloys.org wrote: If you're already doing a bunch of multiplication

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 kwess...@gmail.com wrote: On Wed, 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 huahai.y...@gmail.com wrote: The multiplication outside the reduce is required

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 a...@malloys.org 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

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 a...@malloys.org

Translating Java code with nested for loops

2011-06-28 Thread Bhinderwala, Shoeb
Hi I have been learning clojure for some time but am a bit stumped when translating code with nested for loops. Can someone help me to translate the following java code to clojure elegantly: The inputs are two arrays of type double of the same length - dailyValues and totalValues. The output

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++) {

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 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 = dailyValues.length;