Consider a simple expr
as an example:
expr = {
{a},{a,b},{a,c},{a,b,c,d},{a,b,c,f},
{b,c},{b,d}
};
I want to GroupBy
as much as possible keeping the hierarchy of Keys
intact then apply a function func
on the leaves.
hierarchicalGroupBy[expr, func]
{
a->{b->{c->{func[d],func[f]}},func[c]},
b->{func[c],func[d]}
}
I forgot the case of a (long) branch with a single leaf (below is the updated example):
{
{a},{a,b},{a,b,c,d},{a,b,c,f},{a,b,d,e},{a,b,e,d,c,f},{a,c},
{b,c},{b,d},
{c}
}
This should convert to:
{
a->{b->{c->{func[d],func[f]},d->func[e],e->d->c->func[f]},func[c]},
b->{func[c],func[d]},
func[c]
}