1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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
|