-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchanstest.tcl
More file actions
65 lines (57 loc) · 1.12 KB
/
chanstest.tcl
File metadata and controls
65 lines (57 loc) · 1.12 KB
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
proc gento {max chan} {
for {set i 0} { $i <= $max } { incr i } {
sendchan $chan $i
}
closechan $chan
}
proc gen max {
set ch [newchan]
go gento $max $ch
return $ch
}
proc zip_with {fn ch1 ch2} {
set out [newchan]
set code { { op ch1 ch2 res } {
forchan v $ch1 {
set v2 [<- $ch2]
sendchan $res [$op $v $v2]
}
closechan $res
}}
go [list apply $code $fn $ch1 $ch2 $out]
return $out
}
proc sumchan ch {
set res 0
forchan v $ch {
incr res $v
}
return $res
}
test {sumchan} {
expect [sumchan [gen 100]] == 5050
}
test {zip} {
expect [sumchan [zip_with + [gen 10] [gen 10]]] == 110
}
if false {
proc fibx {n} {
if { $n < 2 } {
return 1
} else {
return [+ [fibx [- $n 1]] [fibx [- $n 2]]]
}
}
proc pfib {n} {
set ch1 [newchan]
set ch2 [newchan]
set code { { n out } {
sendchan $out [fibx $n]
}}
go [list apply $code [- $n 1] $ch1]
go [list apply $code [- $n 2] $ch2]
return [+ [<- $ch1] [<- $ch2]]
}
puts [time { fibx 22 }]
puts [time { pfib 22 }]
}