This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
slice.go
106 lines (93 loc) · 3.08 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
// Copyright 2017 Monax Industries Limited
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package slice
// Convenience function
func Slice(elements ...interface{}) []interface{} {
return elements
}
func EmptySlice() []interface{} {
return []interface{}{}
}
// Like append but on the interface{} type and always to a fresh backing array
// so can be used safely with slices over arrays you did not create.
func CopyAppend(slice []interface{}, elements ...interface{}) []interface{} {
sliceLength := len(slice)
newSlice := make([]interface{}, sliceLength+len(elements))
for i, e := range slice {
newSlice[i] = e
}
for i, e := range elements {
newSlice[sliceLength+i] = e
}
return newSlice
}
// Prepend elements to slice in the order they appear
func CopyPrepend(slice []interface{}, elements ...interface{}) []interface{} {
elementsLength := len(elements)
newSlice := make([]interface{}, len(slice)+elementsLength)
for i, e := range elements {
newSlice[i] = e
}
for i, e := range slice {
newSlice[elementsLength+i] = e
}
return newSlice
}
// Concatenate slices into a single slice
func Concat(slices ...[]interface{}) []interface{} {
offset := 0
for _, slice := range slices {
offset += len(slice)
}
concat := make([]interface{}, offset)
offset = 0
for _, slice := range slices {
for i, e := range slice {
concat[offset+i] = e
}
offset += len(slice)
}
return concat
}
// Deletes n elements starting with the ith from a slice by splicing.
// Beware uses append so the underlying backing array will be modified!
func Delete(slice []interface{}, i int, n int) []interface{} {
return append(slice[:i], slice[i+n:]...)
}
// Delete an element at a specific index and return the contracted list
func DeleteAt(slice []interface{}, i int) []interface{} {
return Delete(slice, i, 1)
}
// Flatten a slice by a list by splicing any elements of the list that are
// themselves lists into the slice elements to the list in place of slice itself
func Flatten(slice []interface{}) []interface{} {
return DeepFlatten(slice, 1)
}
// Recursively flattens a list by splicing any sub-lists into their parent until
// depth is reached. If a negative number is passed for depth then it continues
// until no elements of the returned list are lists
func DeepFlatten(slice []interface{}, depth int) []interface{} {
if depth == 0 {
return slice
}
returnSlice := []interface{}{}
for _, element := range slice {
if s, ok := element.([]interface{}); ok {
returnSlice = append(returnSlice, DeepFlatten(s, depth-1)...)
} else {
returnSlice = append(returnSlice, element)
}
}
return returnSlice
}