Re: group-by replacement when an item has many groups?

2013-07-09 Thread Colin Yates
Thanks Y.Kohyama - it does. On 9 July 2013 03:00, Yoshinori Kohyama wrote: > Hi Colin, > > One more solution, with example data in the process commented > > *(let [f #(range 1 (inc %))* > * coll '(1 2 3)]* > * (->>* > **(for [*x coll*; x = 1, 2, 3 > * y *(*f x*)]* *; y =

Re: group-by replacement when an item has many groups?

2013-07-08 Thread Yoshinori Kohyama
Hi Colin, One more solution, with example data in the process commented *(let [f #(range 1 (inc %))* * coll '(1 2 3)]* * (->>* **(for [*x coll*; x = 1, 2, 3 * y *(*f x*)]* *; y = 1 (where x = 1), **; 1, 2(where x = 2), *

Re: group-by replacement when an item has many groups?

2013-07-08 Thread Colin Yates
Wow - I need to get some sleep: {1 [1 2 3] 2 [2 3] 3 [3]} On 8 July 2013 20:52, Colin Yates wrote: > Perfect, and bonus points for spotting my stupid "not slept in days" > muppetry. The results I want are as you infer: {1 [1 2 3] 2 [1 2] 3 [3]}. > > Now all I have to do is understand your cod

Re: group-by replacement when an item has many groups?

2013-07-08 Thread MichaƂ Marczyk
Another implementation: (defn groups-by [f coll] (apply merge-with into (map (fn [k] (zipmap (f k) (repeat [k]))) coll))) Or with ->>: (defn groups-by [f coll] (->> coll (map (fn [k] (zipmap (f k) (repeat [k] (apply merge-with into))) Could use #(zipmap (f %) (repeat [

Re: group-by replacement when an item has many groups?

2013-07-08 Thread Colin Yates
Hi Jim, I don't think that would give the result I need. Let me give a more realistic example: (defn every-day [[start :start end: end]] ...) ; returns [date1 date2 date3] (def m [{:id 1 :start 1/1/2010 :end 3/1/2010} {:id 2 :start 3/1/2010 :end 3/1/2010}] (group-by every-day m) would give some

Re: group-by replacement when an item has many groups?

2013-07-08 Thread Colin Yates
Perfect, and bonus points for spotting my stupid "not slept in days" muppetry. The results I want are as you infer: {1 [1 2 3] 2 [1 2] 3 [3]}. Now all I have to do is understand your code - time for more coffee I think (not implying your code is difficult, rather the difficultly is a function of

Re: group-by replacement when an item has many groups?

2013-07-08 Thread Ben Wolfson
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

Re: group-by replacement when an item has many groups?

2013-07-08 Thread Jim - FooBar();
you can use group-by as you showed and then reduce-kv over them map replacing each key with '(count key)'...I don't see a way of doing this in one-pass using group-by alone... Jim On 08/07/13 20:25, Colin Yates wrote: Hi, I have a sequence of items and want to group them into categories, th

group-by replacement when an item has many groups?

2013-07-08 Thread Colin Yates
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