1# liststat.tcl -- 2# 3# Set of operations on lists, meant for the statistics package 4# 5# version 0.1: initial implementation, january 2003 6 7namespace eval ::math::statistics {} 8 9# filter -- 10# Filter a list based on whether an expression is true for 11# an element or not 12# 13# Arguments: 14# varname Name of the variable that represents the data in the 15# expression 16# data List to be filtered 17# expression (Logical) expression that is to be evaluated 18# 19# Result: 20# List of those elements for which the expression is true 21# TODO: 22# Substitute local variables in caller 23# 24proc ::math::statistics::filter { varname data expression } { 25 upvar $varname _x_ 26 set result {} 27 set _x_ \$_x_ 28 set expression [uplevel subst -nocommands [list $expression]] 29 foreach _x_ $data { 30 # FRINK: nocheck 31 if $expression { 32 33 lappend result $_x_ 34 } 35 } 36 return $result 37} 38 39# map -- 40# Map the elements of a list according to an expression 41# 42# Arguments: 43# varname Name of the variable that represents the data in the 44# expression 45# data List whose elements must be transformed (mapped) 46# expression Expression that is evaluated with $varname an 47# element in the list 48# 49# Result: 50# List of transformed elements 51# 52proc ::math::statistics::map { varname data expression } { 53 upvar $varname _x_ 54 set result {} 55 set _x_ \$_x_ 56 set expression [uplevel subst -nocommands [list $expression]] 57 foreach _x_ $data { 58 # FRINK: nocheck 59 lappend result [expr $expression] 60 } 61 return $result 62} 63 64# samplescount -- 65# Count the elements in each sublist and return a list of counts 66# 67# Arguments: 68# varname Name of the variable that represents the data in the 69# expression 70# list List of lists 71# expression Expression in that is evaluated with $varname an 72# element in the sublist (defaults to "true") 73# 74# Result: 75# List of transformed elements 76# 77proc ::math::statistics::samplescount { varname list {expression 1} } { 78 upvar $varname _x_ 79 set result {} 80 set _x_ \$_x_ 81 set expression [uplevel subst -nocommands [list $expression]] 82 foreach data $list { 83 set number 0 84 foreach _x_ $data { 85 # FRINK: nocheck 86 if $expression { 87 incr number 88 } 89 } 90 lappend result $number 91 } 92 return $result 93} 94 95# End of list procedures 96