tree_group xs { node_level "id" }What it does:
Divides a list into groups. The first element of the list provides the key value that starts a new group. Example: tree_group {1 2 3 2 1 2 1 1 2 2} = {1 2 3 2} {1 2} 1 {1 2 2} The optional argument node_level is a function that extracts the value of an element. By default, this is the identity function id. Example: tree_group {{a 1} {b 2} {c 1}} snd = {{a 1} {b 2}} {c 1}Defined in: /web/philip/tcl/ad-trees.tcl
Source code:
if { $xs=={} } return {}
set key [$node_level [head $xs]]
set list {}
set sublist {}
foreach x $xs {
if { $sublist=={} || [$node_level $x]!=$key } {
lappend sublist $x
} else {
lappend list $sublist
set sublist [list $x]
}
}
lappend list $sublist
return $list