closuretest.gno
0.67 Kb · 43 lines
1package closuretest
2
3import "strconv"
4
5var (
6 count int
7 stepper func() int
8)
9
10var (
11 accumulator func(int)
12 history []int
13)
14
15func init() {
16 step := 3
17 stepper = func() int {
18 count += step
19 return count
20 }
21
22 maxLen := 10
23 history = make([]int, 0, maxLen)
24 accumulator = func(val int) {
25 if len(history) < maxLen {
26 history = append(history, val)
27 }
28 }
29}
30
31func Step() string {
32 result := stepper()
33 return "count=" + strconv.Itoa(result)
34}
35
36func Accumulate(val int) string {
37 accumulator(val)
38 return "history length=" + strconv.Itoa(len(history))
39}
40
41func Render(_ string) string {
42 return "closuretest: count=" + strconv.Itoa(count) + " history=" + strconv.Itoa(len(history))
43}