(* 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 <[email protected]> wrote:
> 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 <[email protected]> wrote:
>
>
>
>
>
>
>
> > 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))
>
> > > > >         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];
> > > > >         }

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to