Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

How can GatherBy be programmed to take an option to group results as follows:

data = {T0->1993-09-29,T1->1993-10-04,T10->1993-09-29,T11->1993-10-01,T12->1993-10-02,T13->1993-10-04,T14->1992-10,T3->1991,T4->1991,T7->1992-10,T8->1992-10,T9->1993-09-26}

Column@(data2 = GatherBy[data, #[[2]] & ]) gives:

{T0->1993-09-29,T10->1993-09-29}
{T1->1993-10-04,T13->1993-10-04}
{T11->1993-10-01}
{T12->1993-10-02}
{T14->1992-10,T7->1992-10,T8->1992-10}
{T3->1991,T4->1991}
{T9->1993-09-26}

It's desired to optionally group into lists based on the common right-hand sides:

Column[ ((First /@ #) -> #[[1, 2]]) & /@ data2], which gives:

{T0,T10}->1993-09-29
{T1,T13}->1993-10-04
{T11}->1993-10-01
{T12}->1993-10-02
{T14,T7,T8}->1992-10
{T3,T4}->1991
{T9}->1993-09-26

And to do this not just for Rule expression arguments but for any argument and GatherBy test functions.

share|improve this question
4  
It's not clear to me why you insist on adding syntax to existing built-in functions. Instead, you can write your own gatherBy, and introduce any syntax you like. The essential part of your question can be answered similarly to what I did for your previous one, but I guess this type of solution is not what you are after. –  Leonid Shifrin Jun 21 '12 at 19:58
    
Who wants to write and look at code with myGatherBy all over the place. Are you kidding me? –  alancalvitti Jun 21 '12 at 20:14
7  
It depends on what you do. I don't hesitate to introduce my own functions and use them, and so do many others. If you design some functionality for others, then you may have to think about this. But in any case, overloading built-in functions is a bad idea, mainly because you never know what you change. It has been discussed and proven many times that tiny convenience benefits of doing this are likely to cause serious trouble later. The only technique I know which can allow you to safely change the syntax is preprocessors/code generation, but in Mathematica there is no real compile stage. –  Leonid Shifrin Jun 21 '12 at 20:29
2  
@LeonidShifrin: True that. –  István Zachar Jun 21 '12 at 21:00
2  
gatherBy[expr_,f_,postf_]:=Map[postf,GatherBy[expr,f]], for a start. You seem to want some postprocessing. –  Leonid Shifrin Jun 21 '12 at 22:09

1 Answer 1

The following is not intended as an answer but as a helper to transform the example data provided by OP to strings to avoid data elements like 1993 - 09 - 29 evaluating to 1955.

trnsfrmddata0 = 
Map[ToString, ({T0 -> 1993 - 09 - 29, T1 -> 1993 - 10 - 04, 
    T10 -> 1993 - 09 - 29, T11 -> 1993 - 10 - 01, 
    T12 -> 1993 - 10 - 02, T13 -> 1993 - 10 - 04, 
    T14 -> 1992 - 10, T3 -> 1991, T4 -> 1991, T7 -> 1992 - 10, 
    T8 -> 1992 - 10, T9 -> 1993 - 09 - 26} // HoldForm) /. 
 HoldPattern[Plus[t__]] :> List[t], {-1}] /. 
 Rule[aa_, bb_] :> Rule[aa, StringJoin[bb]] // ReleaseHold

gives

{"T0" -> "1993-9-29", "T1" -> "1993-10-4", "T10" -> "1993-9-29", "T11" -> "1993-10-1", "T12" -> "1993-10-2", "T13" -> "1993-10-4", "T14" -> "1992-10", "T3" -> "1991", "T4" -> "1991", "T7" -> "1992-10", "T8" -> "1992-10", "T9" -> "1993-9-26"}

Alternatively, one can use DateList[StringJoin[bb]] instead of StringJoin[bb] inthe last line to get datelists as the RHS of rules.

With this transformation of the example data,

 Column[trdata1 = GatherBy[#, Last] &@trnsfrmddata0]

gives

enter image description here

and

Column[trdata1 /. pat : {Rule[_, _] ..} :> Rule[First /@ pat, Last@First@pat]]
(* or *)
Column[((First /@ #) -> #[[1, 2]]) & /@ trdata1]

both give

enter image description here

share|improve this answer
    
thanks, that's nice work. the original data both the left and right sides of Rule are strings but when copying as plain text, they get pasted without the quotes. Is there a better way to copy/paste? –  alancalvitti Jun 22 '12 at 0:47
    
@Alan, I am not sure but Copy As >> PlainText may be the culprit; plain copy (Ctrl-C) and paste works fine in most cases. –  kguler Jun 22 '12 at 6:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.