-
Notifications
You must be signed in to change notification settings - Fork 2
/
controlFlow-0.tm
61 lines (49 loc) · 1.05 KB
/
controlFlow-0.tm
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
package provide controlFlow 0
proc do {body while_or_until condition {rest {}}} {
if {$while_or_until eq "while"} {
set break_test [list uplevel expr ! ( $condition ) ]
} elseif {$while_or_until eq "until"} {
set break_test [list uplevel expr $condition ]
} else {
error "Syntax error: do body while condition or do body until condition"
}
while {true} {
uplevel $body
if {[eval $break_test]} {
break
}
uplevel $rest
}
}
proc seq { low high } {
set result {}
for { set i $low } { $i <= $high } { incr i } {
lappend result $i
}
return $result
}
proc iforeach { index_var value_var list body } {
upvar $index_var index
upvar $value_var value
set index 0
foreach value $list {
uplevel $body
incr index
}
}
proc doto { value commandList } {
foreach command $commandList {
uplevel [concat $value $command]
}
}
proc flatmap { varlist list body } {
foreach varname $varlist {
upvar $varname $varname
}
set result {}
foreach $varlist $list {
set values [uplevel $body]
lappend result {*}$values
}
return $result
}