Here's a similar idea (also untested)

(defn which-list [number]
                  (let [by-5 (zero? (mod number 5))]
                           (cond
                                 (and (odd? number) by-5) [:odd-5]
                                 by-5 [:even-5]
                                (odd? number) [:odd]
                                :else [:even])))

(reduce #(update-in %1 (which-list %2) conj %2)
               {}
               numbers)



On Feb 28, 8:03 am, "Heinz N. Gies" <he...@licenser.net> 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
>
> On Feb 28, 2010, at 2:54 , logan wrote:
>
> > Let's say I want to write a function that takes as an argument a list
> > of n numbers, and returns 4 lists, a list of odd numbers that are
> > multiples of 5, a list of even numbers that are multiples of 5, a list
> > of odd numbers that are not multiples of 5, and a list of even numbers
> > that are not multiples of 5.
>
> > If I were writing this function procedurally, I could create 4 empty
> > lists, and iterate through my input list once, using a compound if
> > statement to add to the appropriate list.
>
> > In functional style, I could use filter, but I can't seem to think of
> > a solution that doesn't end up iterating through the whole list
> > multiple times.
>
> > Can someone please teach me what is the correct clojure approach?
> > Thanks.
>
> > --
> > 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

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