forked from goplus/llgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genints.go
63 lines (55 loc) · 846 Bytes
/
genints.go
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
package main
import (
"github.com/goplus/llgo/c"
)
type generator struct {
val c.Int
}
func (g *generator) next() c.Int {
g.val++
return g.val
}
func genInts(n int, gen func() c.Int) []c.Int {
a := make([]c.Int, n)
for i := range a {
a[i] = gen()
}
return a
}
func main() {
// generate 5 random integers
for _, v := range genInts(5, c.Rand) {
c.Printf(c.Str("%d\n"), v)
}
// generate 5 integers, each is double of the previous one
initVal := c.Int(1)
ints := genInts(5, func() c.Int {
initVal *= 2
return initVal
})
for _, v := range ints {
c.Printf(c.Str("%d\n"), v)
}
// generate 5 integers, each is incremented by 1
g := &generator{val: 1}
for _, v := range genInts(5, g.next) {
c.Printf(c.Str("%d\n"), v)
}
}
/* Posible output:
16807
282475249
1622650073
984943658
1144108930
2
4
8
16
32
2
3
4
5
6
*/