forked from ahmetb/go-linq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
166 lines (154 loc) · 2.74 KB
/
util.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package linq
import "reflect"
func takeSliceArg(arg T) (out []T, ok bool) {
slice, success := takeArg(arg, reflect.Slice)
if !success {
ok = false
return
}
c := slice.Len()
out = make([]T, c)
for i := 0; i < c; i++ {
out[i] = slice.Index(i).Interface()
}
return out, true
}
func takeArg(arg T, kind reflect.Kind) (val reflect.Value, ok bool) {
val = reflect.ValueOf(arg)
if val.Kind() == kind {
ok = true
}
return
}
func toInts(in []T) ([]int, error) {
dst := make([]int, len(in))
var ok bool
for i, v := range in {
var r int
if r, ok = v.(int); !ok {
return nil, ErrTypeMismatch
}
dst[i] = r
}
return dst, nil
}
func toStrings(in []T) ([]string, error) {
dst := make([]string, len(in))
var ok bool
for i, v := range in {
var r string
if r, ok = v.(string); !ok {
return nil, ErrTypeMismatch
}
dst[i] = r
}
return dst, nil
}
func toFloat64s(in []T) ([]float64, error) {
dst := make([]float64, len(in))
var ok bool
for i, v := range in {
var r float64
if r, ok = v.(float64); !ok {
return nil, ErrTypeMismatch
}
dst[i] = r
}
return dst, nil
}
func intsToInterface(in []int) []T {
dst := make([]T, len(in))
for i := 0; i < len(in); i++ {
dst[i] = in[i]
}
return dst
}
func float64sToInterface(in []float64) []T {
dst := make([]T, len(in))
for i := 0; i < len(in); i++ {
dst[i] = in[i]
}
return dst
}
func stringsToInterface(in []string) []T {
dst := make([]T, len(in))
for i := 0; i < len(in); i++ {
dst[i] = in[i]
}
return dst
}
func minMaxInts(in []T) (int, int, error) {
var (
ok bool
minVal, maxVal int
min, max int
minSet, maxSet bool
)
for i, v := range in {
var r int
if r, ok = v.(int); !ok {
return -1, -1, ErrTypeMismatch
}
if r < minVal || !minSet {
minVal = r
min = i
minSet = true
}
if r > maxVal || !maxSet {
maxVal = r
max = i
maxSet = true
}
}
return min, max, nil
}
func minMaxUints(in []T) (int, int, error) {
var (
ok bool
minVal, maxVal uint
min, max int
minSet, maxSet bool
)
for i, v := range in {
var r uint
if r, ok = v.(uint); !ok {
return -1, -1, ErrTypeMismatch
}
if r < minVal || !minSet {
minVal = r
min = i
minSet = true
}
if r > maxVal || !maxSet {
maxVal = r
max = i
maxSet = true
}
}
return min, max, nil
}
func minMaxFloat64s(in []T) (int, int, error) {
var (
ok bool
minVal, maxVal float64
min, max int
minSet, maxSet bool
)
for i, v := range in {
var r float64
if r, ok = v.(float64); !ok {
return -1, -1, ErrTypeMismatch
}
if r < minVal || !minSet {
minVal = r
min = i
minSet = true
}
if r > maxVal || !maxSet {
maxVal = r
max = i
maxSet = true
}
}
return min, max, nil
}