Group curves

Design script definition that groups a list of shuffled curves into loops…
def grpCrvs(crv:var[]..[])
{
dis1 = (crv.StartPoint)<1>.DistanceTo(crv<2>)==0
||(crv.EndPoint)<1>.DistanceTo(crv<2>)==0;
ind1 = List.AllIndicesOf(dis1<1>,true);
bln1 = true;
ind2 = [Imperative]
{
while (bln1)
{
cnt1 = List.Count(ind1);
ind1 = grpIndx(ind1);
cnt2 = List.Count(ind1);
bln1 = cnt2!=cnt1;
}
return = ind1;
}
crv1 = List.GetItemAtIndex(crv,ind2);
return = crv1;
};
def grpIndx(ind:var[]..[])
{
ind1 = List.SetIntersection(ind<1>,ind<2>);
cnt1 = List.Count(ind1<1><2>)>0;
ind2 = List.FilterByBoolMask(ind,cnt1<1>)["in"];
ind3 = List.UniqueItems(List.Flatten(ind2<1>,-1)<1>);
ind4 = List.UniqueItems(List.Sort(ind3<1>));
return = ind4;
};