[PN] 2007.10.20 This page is dedicated to Knuth sections 7.2.1.1 and 7.2.1.2 All the routines on this page generate a sequence of objects, so they are all objects of an iterative type. An ''iterate'' type provides for 3 basic functions, ''init'', ''next'' and ''more''. ''init'' gets things rolling and returns the first value in the sequence. ''next'' gives you the next value in the sequence or an invalid value ''more'' tells you if the last value you just got with ''init'' or ''next'' is a valid value or not. Typically a value of an empty list is the standard invalid value, Because all these routines return a sequence of values which mean something in relation to each other, {} is never a valid returned value. However in some cases {{}},the empty set, may be returned as a valid value in some cases. The ''iterate'' command returns a handle to an interative type of the kind that you have requested in the command. There is a seemingly endless variety of these as this page will demonstrate. The handle is ''it'' followed by a self appointed number, e.g. ''it4''. This handle is an iterative object and is typically invoked in this way: set o [iterate ....] for {set i [$o init]} {[$o more]} {set i [$o next]} { do something with $i } "type delete" $o When you are iterating a number of sequences in the same program this form of expression is easier to use than the namespace version. Because they all have the same ''init'', ''next'' and ''more'' commands, the commands have to be disambiguated by using the object name all the time. Therefore these iterative objects have been implemented in the form of something I call a '''type'''. A type consists of a namespace for the object, plus, in the case of these iterative types, the 3 commands implemented as iterative procedures: "$o init", "$o next" and "$o more". To avoid having to use quotes everywhere you use the '''type create''' command to make these availbale at a more readable level. The contents of the namespace remains private. The '''iterate''' command uses '''type create''' inside it as part of creating the iterative object. For now I have decided not to have the objects self-destruct when they have completed their tour of duty, just in case the user wants to start them from the begining again. So for now the user is responsible for destroying the object. You do this using the '''type delete''' command as seen above. This deletes the namespace ''::$o'', the command ''$o'' which was created by ''[["type create"]]'' and the commands ''[["$o *"]]''. To begin, here are the [[type]] commands. I have decided not to apply ''type'' to ''type'' itself, though that is totally valid and would make type into a universal metaobject. '''[["type create"]]''' is not used much and is usually hidden inside something else. '''{{"type delete"]]''' is easy enough to type the few times you need it. I also choose not to type ''[[type]]'' because this name is probably polluted enough already. In [TOOT] this same ''[["type create"]]'' command is used to replace the ''[[unknown]]'' command. This has the effect of making everything potentially typed. I choose not to do this because I like to have more control over what is typed and what isn't. Also I already reuse ''[[unkown]]'' to '''expand''' the inital command word (instead of compacting the first two words which is the intention of [[type create]]) so as to enable currying within lambda expressions. If you try to do both without careful structure you can end up in a curry/type loop, forever expanding and contracting the command word. proc "type create" t { set str " proc $t {cmd args} { set p \"$t \$cmd\"" append str { if { [ info proc $p ] == {} } { global errorInfo error "No proc: $p" $errorInfo return } uplevel 1 [ linsert $args 0 $p ] } append str " }" eval $str } proc "type delete" t { namespace delete $t foreach i [info proc "$t *"] {rename $i {} } rename $t {} }