-
Notifications
You must be signed in to change notification settings - Fork 14
/
51.go
66 lines (58 loc) · 1.48 KB
/
51.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
64
65
66
package main
import . "fmt"
import "reflect"
type List struct { int; *List }
func main() {
t := &List{4, &List{6, &List{8, nil}}}
Range(struct { X, Y int; List }{0, 2, *t}, print_cell)
Range(struct { *List; Y, X int }{ t, 2, 0 }, print_cell)
Range(List{0, &List{2, t}}, print_cell)
}
func print_cell(i int, v interface{}) {
switch s := v.(type) {
case struct { X, Y int; List }:
Printf("%v: (%v %v %v)\n", i, s.X, s.Y, s.List)
case struct { *List; Y, X int }:
print_cell(i, struct { X, Y int; List } { s.X, s.Y, *s.List })
case List:
Printf("%v: %v\n", i, s.int)
case *List:
print_cell(i, *s)
default:
Printf("%v: %v\n", i, v)
}
}
func copy_value(v reflect.Value) reflect.Value {
rv := reflect.New(v.Type()).Elem()
rv.Set(v)
return rv
}
func copy_data(t reflect.Type, s reflect.Value) (r reflect.Value) {
r = copy_value(s)
switch l := r.FieldByName("List"); {
case !l.IsValid():
case l.Kind() == reflect.Ptr:
l.Set(reflect.New(l.Type().Elem()))
default:
l.Set(reflect.New(l.Type()).Elem())
}
return
}
func next_node(t reflect.Type, s reflect.Value) (r reflect.Value) {
switch f := s.FieldByName("List"); f.Type() {
case t, reflect.PtrTo(t):
r = reflect.Indirect(f)
}
return
}
func range_list(s reflect.Value, f func(int, interface{})) {
t := reflect.Indirect(s).Type()
for i := 0; s.IsValid(); i++ {
link := next_node(t, s)
f(i, copy_data(t, s).Interface())
s = link
}
}
func Range(s interface{}, f func(int, interface{})) {
range_list(reflect.ValueOf(s), f)
}