On Feb 28, 4:03 pm, "Heinz N. Gies" <[email protected]> wrote:
> How about reducing it?
>
> Something along the limes of: (not tested)
>
> (reduce (fn [lists number]
>         (update-in lists [
>           (if (odd? number)
>           (if (= 0 (mod number 5) :odd-5 :odd)
>           (if (= 0 (mod number 5) :even-5 :even)] (fn [l] (conj l number)) 
> numbers)
>
> Regards,
> Heinz

I think that's a pretty good solution. However, some things I
noticed:

1) zero? instead of (= 0 ...) is more idiomatic and a bit faster
2) You don't need to wrap the conj in an anonymous function. update-in
accepts optional arguments, so (update-in [...] conj number) works
just fine.
3) The reduce needs an empty map as an init value. Otherwise you get
an exception (You might even want to populate it with empty vectors,
but it's not necessary since conjing to nil produces a list)

Also, the nested ifs are pretty hard to follow, so I think it would be
clearer if you made the key selection a local function, so that you
can write (update-in [(categorize number)] ...)

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