Skip to content

group-by should support multiple grouper arguments #14330

@Bahex

Description

@Bahex

Related problem

Related:

Describe the solution you'd like

Similar to sort-by, group-by should support multiple grouper arguments, as the rest parameter.

group-by ...(grouper)

Examples will be demonstrated on this sample data, taken from split-by's examples

let data = [
    [name, lang, year];
    [andres, rb, "2019"],
    [jt, rs, "2019"],
    [storm, rs, "2021"]
]

Without --to-table, the output would be in the form of nested records, which would also make split-by obsolete.

Output of the split-by command, and the suggested group-by should be the same.

> $data | group-by year | split-by lang
> $data | group-by lang year
╭────┬─────────────────────────────────────────╮
     ╭──────┬──────────────────────────────╮ 
 rb         ╭─#─┬──name──┬─lang─┬─year─╮  
      2019   0  andres  rb    2019   
            ╰─#─┴──name──┴─lang─┴─year─╯  
     ╰──────┴──────────────────────────────╯ 
     ╭──────┬─────────────────────────────╮  
 rs         ╭─#─┬─name─┬─lang─┬─year─╮    
      2019   0  jt    rs    2019     
            ╰─#─┴─name─┴─lang─┴─year─╯    
            ╭─#─┬─name──┬─lang─┬─year─╮   
      2021   0  storm  rs    2021    
            ╰─#─┴─name──┴─lang─┴─year─╯   
     ╰──────┴─────────────────────────────╯  
╰────┴─────────────────────────────────────────╯

With --to-table, the output should be a flat table (excluding items column) regardless of how many grouper arguments are used.

> $data | group-by lang year --to-table
╭─#─┬─group_1─┬─group_2─┬────────────items─────────────╮
 0  rb       2019     ╭─#─┬──name──┬─lang─┬─year─╮ 
                       0  andres  rb    2019  
                      ╰─#─┴──name──┴─lang─┴─year─╯ 
 1  rs       2019     ╭─#─┬─name─┬─lang─┬─year─╮   
                       0  jt    rs    2019    
                      ╰─#─┴─name─┴─lang─┴─year─╯   
 2  rs       2021     ╭─#─┬─name──┬─lang─┬─year─╮  
                       0  storm  rs    2021   
                      ╰─#─┴─name──┴─lang─┴─year─╯  
╰─#─┴──lang───┴──year───┴────────────items─────────────╯

Note: The columns will be named after the groupers' string form, and group{i} will be used for groupers that can't be stringified (closures).

Describe alternatives you've considered

No response

Additional context and details

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions