wdb Proposal for lfiter which fits harmonical to other built-in list procedures (lsearch, lsort, lmap, etc.) Takes as arguments optional switch -indices to deliver indices instead of elements. Takes then list to filter, then lambda term which contains formal variable and lambda body.
Example:
% set l {4 3 99 5 20 2 11}
4 3 99 5 20 2 11
% lfilter $l {x {expr {$x % 2 == 0}}}
4 20 2Code:
# lfilter ?-indices? list lambda
proc lfilter args {
if {[llength $args] == 3} then {
lassign $args opt li lambda
if {$opt eq "-indices"} then {
set indices true
} else {
return -code error [list only -indices but not $opt allowed]
}
} else {
lassign $args li lambda
set indices false
}
set result {}
set i 0
foreach el $li {
if {[apply $lambda $el]} then {
if {$indices} then {
lappend result $i
} else {
lappend result $el
}
}
incr i
}
set result
}