See Also edit
- the filter idiom
- Tcl's simple syntax for reading and writing to stdin
- How can I run data through an external filter?
- a wrapper to handle the details of pipeline filtering:
Filter in functional programming edit
RS 2005-03-08: filter is also a classic operator in functional programming, and it makes nice simple code:proc filter {list script} {
set res {}
foreach e $list {if {[uplevel 1 $script $e]} {lappend res $e}}
set res
}Testing, with a simple way to do intersection:proc in {list e} {expr {[lsearch -exact $list $e]>=0}}
% filter {a b c} {in {b c d}}
b c[lpz] 2013-06-09: The elements I use, have spaces, so I changed the filter proc to
proc filter {list script} {
set res {}
foreach e $list {if {[uplevel 1 $script [list $e]]} {lappend res $e}}
set res
}Testing% filter { {s pace} x d} {in {{s pace} a b c d e}}
{s pace} d[smh2] 2006-08-26: Here is a proc based on this to find the intersection of many lists:
proc intersection { items } {
set res [lindex $items 0] ; # start with the first sub-list
foreach item [lrange $items 1 end] { ; # loop through the rest
set res [filter $res {in $item}] ; # and filter each new subset against the next
}
set res
}AMG: Implementation using lcomp:
proc filter {list test_expression} {
lcomp {$x} for x in $list if $test_expression
}test_expression is a boolean if expression that should be a function of $x, the list element being tested.
