You could do something like this, which is just a generalization of
group-by to the multiple value case (except that group-by actually uses
transients):

user> (defn groups-by [f coll]
        (reduce (fn [acc x]
                    (let [ks (f x)]
                      (reduce (fn [acc' k] (update-in acc' [k] #(conj (or %
[]) x))) acc ks)))
                {} coll))
#'user/groups-by
user> (groups-by #(range 1 (inc %)) [1 2 3])
{3 [3], 2 [2 3], 1 [1 2 3]}

(This isn't the return value you say you want, but it seems to be the right
return value, unless I've misunderstood the problem: 1, 2, and 3 all
include 1, but only 3 includes 3.)

You can define regular group-by in terms of this function as (defn group-by
[f coll] (groups-by (comp vector f) coll)).



On Mon, Jul 8, 2013 at 12:25 PM, Colin Yates <colin.ya...@gmail.com> wrote:

> Hi,
>
> I have a sequence of items and want to group them into categories, the
> value of which is a function of the item.  This sounds exactly what
> group-by is after.
>
> The kicker is that the function could return multiple values.  Imagine
> each item was a date range and I wanted to group them by the number of days
> in that date range.
>
> For example, (group-by #(range 1 (inc %)) [1 2 3] => {(1) [1] (1 2) [2] (1
> 2 3) [3]}.  I want {1 [1] 2 [1 2] 3 [1 2 3]}.
>
> Any ideas?
>
> Thanks!
>
> Col
>
> --
> --
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>



-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to