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-indexed
     (fn [i daily]
       (reduce #(* %1 (inc (/ %2 100.0)))
               daily
               (drop (inc i) total-values)))
     daily-values))

And I'm not sure, but I think it's clearer what's going on if you pull
the manipulation of the total-values into a separate map, and just
reduce with a simple multiplication:

(defn calc [total-values daily-values]
  (map-indexed
     (fn [i daily]
       (reduce * daily
               (map #(inc (/ % 100.0))
                    (drop (inc i) total-values))))
     daily-values))

And once you've done that, you can pretty simply reduce the amount of
nesting to define this as a threaded pipeline of operations starting
with total-values:

(defn calc [total-values daily-values]
  (map-indexed
   (fn [i daily]
     (->> total-values
          (drop (inc i))
          (map #(inc (/ % 100.0)))
          (reduce * daily)))
     daily-values))

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]
  (let [interesting-values (next total-values)]
    (map-indexed
     (fn [i daily]
       (->> total-values
            (drop i)
            (map #(inc (/ % 100.0)))
            (reduce * daily)))
     daily-values)))

On Jun 28, 11:03 pm, hci <huahai.y...@gmail.com> wrote:
> 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> wrote:
>
>
>
>
>
>
>
> > 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
> >                                 (rest tail)))))
>
> > In translating it, I first tried to visualize the algorithm[1]. Then I
> > transcribed that visualization into the usual suspects: map/for,
> > reduce, filter. Having a solid grasp of each of those -- not to
> > mention the rest of clojure.core -- is very helpful.
>
> > [1]http://i.imgur.com/XDZhm.png(crudedrawing of the first step)
>
> > Hope that helps,
>
> > Justin
>
> > On Jun 28, 8:20 pm, "Bhinderwala, Shoeb"
>
> > <sabhinderw...@wellington.com> wrote:
> > > 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 is the array contrib of the same
> > > length.
>
> > >         int n = dailyValues.length;
>
> > >         for (int i = 0; i < n; i++)
> > >         {
> > >             sum = 1.0;
> > >             for (int j = i + 1; j < n; j++)
> > >             {
> > >                 sum *= (1.0 + (totalValues[j] / 100.0));
> > >             }
>
> > >             contrib[i] = sum * dailyValues[i];
> > >         }
>
> > > Many thanks for your help.
>
> > > -- Shoeb

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