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 =
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),
*
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
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 [
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
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
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
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
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