-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslice.go
249 lines (217 loc) · 5.2 KB
/
slice.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package goramda
import (
// "go/types"
"fmt"
"reflect"
)
// TODO: following have to implement
/*
Head
Tail
Nth
IndexOf
LastIndexOf
Find
FindLast
FindIndex
FindLastIndex
Adjust
All
Any
Aperture
Concat
Contains
Drop
DropLast
DropLastWhile
DropRepeats
DropWhile
EndsWith
Filter
Update
*/
var typeError func(expected, given interface{}) error = func(e, g interface{}) error {
return fmt.Errorf("Type mismatch expected %T got %T", e, g)
}
// Returns the first element of the given list or string. In some libraries this function is named first.
func Head(d interface{}) interface{} {
arrV := reflect.ValueOf(d)
switch arrV.Kind() {
case reflect.String, reflect.Slice:
return nth(0, arrV)
case reflect.Ptr:
return nil
default:
return getDefaultValueOf(d)
}
}
func finder(fn func(d interface{}) bool, list interface{}) (interface{}, int) {
if isSlice(list) {
arrV := reflect.ValueOf(list)
for i := 0; i < arrV.Len(); i++ {
v := arrV.Index(i).Interface()
if fn(v) {
return v, i
}
}
}
return nil, -1
}
func find(fn func(d interface{}) bool, list interface{}) interface{} {
v, _ := finder(fn, list)
return v
}
func findIndex(fn func(d interface{}) bool, list interface{}) int {
_, index := finder(fn, list)
return index
}
func Find(fn func(d interface{}) bool) func(list interface{}) interface{} {
return func(list interface{}) interface{} {
return find(fn, list)
}
}
func FindIndex(fn func(d interface{}) bool) func(list interface{}) int {
return func(list interface{}) int {
return findIndex(fn, list)
}
}
func isSlice(d interface{}) bool {
return reflect.ValueOf(d).Kind() == reflect.Slice
}
func Tail(d interface{}) interface{} {
arrV := reflect.ValueOf(d)
switch arrV.Kind() {
case reflect.Slice:
return drop(0, arrV.Len(), d)
case reflect.String:
return trimFirstRune(String(d))
case reflect.Ptr:
return nil
default:
return getDefaultValueOf(d)
}
}
func Last(d interface{}) interface{} {
arrV := reflect.ValueOf(d)
switch arrV.Kind() {
case reflect.String, reflect.Slice:
return nth(arrV.Len()-1, arrV)
case reflect.Ptr:
return nil
default:
return getDefaultValueOf(d)
}
}
func trimFirstRune(s string) string {
for i := range s {
if i > 0 {
// The value i is the index in s of the second
// rune. Slice to remove the first rune.
return s[i:]
}
}
return ""
}
func nth(index int, arrV reflect.Value) interface{} {
if isIndexable(index, arrV) {
return arrV.Index(index).Interface()
}
return nil
}
func isIndexable(index int, arrV reflect.Value) bool {
ok := (arrV.Kind() == reflect.Slice || arrV.Kind() == reflect.String) &&
arrV.Len() > index && index >= 0
return ok
}
func IndexOf(val interface{}, d interface{}) int {
return contains(val, d)
}
func LastIndexOf(val interface{}, d interface{}) int {
return lastContains(val, d)
}
func Nth(index int, d interface{}) interface{} {
arrV := reflect.ValueOf(d)
return nth(index, arrV)
}
// Includes checks if a given slice of elements contains the provided single element value.
// if element available return true
func Includes(s, elem interface{}) bool {
return contains(s, elem) >= 0
}
func contains(s, elem interface{}) int {
arrV := reflect.ValueOf(s)
if arrV.Kind() == reflect.Slice {
for i := 0; i < arrV.Len(); i++ {
// XXX - panics if slice element points to an unexported struct field
// see https://golang.org/pkg/reflect/#Value.Interface
if arrV.Index(i).Interface() == elem {
return i
}
}
}
return -1
}
func lastContains(s, elem interface{}) int {
arrV := reflect.ValueOf(s)
if arrV.Kind() == reflect.Slice {
for i := arrV.Len() - 1; i >= 0; i-- {
if arrV.Index(i).Interface() == elem {
return i
}
}
}
return -1
}
func drop(start, end int, s interface{}) interface{} {
n := reflect.ValueOf(s)
rest := reflect.MakeSlice(reflect.TypeOf(s), 0, 0)
for i := start; i < end; i++ {
rest = reflect.Append(rest, n.Index(i))
}
r := rest.Interface()
return r
}
func Drop(count int, d interface{}) interface{} {
v := reflect.ValueOf(d)
if v.Kind() != reflect.Slice {
return d
}
return drop(count, v.Len(), d)
}
func DropLast(count int, d interface{}) interface{} {
v := reflect.ValueOf(d)
if v.Kind() != reflect.Slice {
return d
}
return drop(0, v.Len()-(1+count), d)
}
func Append(arrV interface{}, s ...interface{}) interface{} {
n := reflect.ValueOf(arrV)
n2 := reflect.ValueOf(s)
newV := reflect.MakeSlice(reflect.TypeOf(s), 0, 0)
for i := 0; i < n.Len(); i++ {
newV = reflect.Append(newV, n.Index(i))
}
for i := 0; i < n2.Len(); i++ {
newV = reflect.Append(newV, n2.Index(i))
}
return newV.Interface()
}
// func All(condFunc func(v, iter interface{}) bool, list interface{}) bool {
// condFunc
// }
// Applies a function to the value at the given index of an array, returning a new
// copy of the array with the element at the given index replaced with the result of
// the function application.
// func Adjust() interface{} {
// arrV := reflect.ValueOf(s)
// if arrV.Kind() == reflect.Slice {
// for i := 0; i < arrV.Len(); i++ {
// // XXX - panics if slice element points to an unexported struct field
// // see https://golang.org/pkg/reflect/#Value.Interface
// if arrV.Index(i).Interface() == elem {
// return true
// }
// }
// }
// }