diff options
Diffstat (limited to 'tcllib/modules/math/liststat.tcl')
-rwxr-xr-x | tcllib/modules/math/liststat.tcl | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/tcllib/modules/math/liststat.tcl b/tcllib/modules/math/liststat.tcl new file mode 100755 index 0000000..d7b2e14 --- /dev/null +++ b/tcllib/modules/math/liststat.tcl @@ -0,0 +1,95 @@ +# liststat.tcl -- +# +# Set of operations on lists, meant for the statistics package +# +# version 0.1: initial implementation, january 2003 + +namespace eval ::math::statistics {} + +# filter -- +# Filter a list based on whether an expression is true for +# an element or not +# +# Arguments: +# varname Name of the variable that represents the data in the +# expression +# data List to be filtered +# expression (Logical) expression that is to be evaluated +# +# Result: +# List of those elements for which the expression is true +# TODO: +# Substitute local variables in caller +# +proc ::math::statistics::filter { varname data expression } { + upvar $varname _x_ + set result {} + set _x_ \$_x_ + set expression [uplevel subst -nocommands [list $expression]] + foreach _x_ $data { + # FRINK: nocheck + if $expression { + + lappend result $_x_ + } + } + return $result +} + +# map -- +# Map the elements of a list according to an expression +# +# Arguments: +# varname Name of the variable that represents the data in the +# expression +# data List whose elements must be transformed (mapped) +# expression Expression that is evaluated with $varname an +# element in the list +# +# Result: +# List of transformed elements +# +proc ::math::statistics::map { varname data expression } { + upvar $varname _x_ + set result {} + set _x_ \$_x_ + set expression [uplevel subst -nocommands [list $expression]] + foreach _x_ $data { + # FRINK: nocheck + lappend result [expr $expression] + } + return $result +} + +# samplescount -- +# Count the elements in each sublist and return a list of counts +# +# Arguments: +# varname Name of the variable that represents the data in the +# expression +# list List of lists +# expression Expression in that is evaluated with $varname an +# element in the sublist (defaults to "true") +# +# Result: +# List of transformed elements +# +proc ::math::statistics::samplescount { varname list {expression 1} } { + upvar $varname _x_ + set result {} + set _x_ \$_x_ + set expression [uplevel subst -nocommands [list $expression]] + foreach data $list { + set number 0 + foreach _x_ $data { + # FRINK: nocheck + if $expression { + incr number + } + } + lappend result $number + } + return $result +} + +# End of list procedures |