forked from beego/beego
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slice.go
101 lines (83 loc) · 3.19 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
// Copyright 2014 beego Author. All Rights Reserved.
//
// 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 utils
import (
"github.com/Sonek-HoangBui/beego/v2/core/utils"
)
type reducetype func(interface{}) interface{}
type filtertype func(interface{}) bool
// InSlice checks given string in string slice or not.
func InSlice(v string, sl []string) bool {
return utils.InSlice(v, sl)
}
// InSliceIface checks given interface in interface slice.
func InSliceIface(v interface{}, sl []interface{}) bool {
return utils.InSliceIface(v, sl)
}
// SliceRandList generate an int slice from min to max.
func SliceRandList(min, max int) []int {
return utils.SliceRandList(min, max)
}
// SliceMerge merges interface slices to one slice.
func SliceMerge(slice1, slice2 []interface{}) (c []interface{}) {
return utils.SliceMerge(slice1, slice2)
}
// SliceReduce generates a new slice after parsing every value by reduce function
func SliceReduce(slice []interface{}, a reducetype) (dslice []interface{}) {
return utils.SliceReduce(slice, func(i interface{}) interface{} {
return a(i)
})
}
// SliceRand returns random one from slice.
func SliceRand(a []interface{}) (b interface{}) {
return utils.SliceRand(a)
}
// SliceSum sums all values in int64 slice.
func SliceSum(intslice []int64) (sum int64) {
return utils.SliceSum(intslice)
}
// SliceFilter generates a new slice after filter function.
func SliceFilter(slice []interface{}, a filtertype) (ftslice []interface{}) {
return utils.SliceFilter(slice, func(i interface{}) bool {
return a(i)
})
}
// SliceDiff returns diff slice of slice1 - slice2.
func SliceDiff(slice1, slice2 []interface{}) (diffslice []interface{}) {
return utils.SliceDiff(slice1, slice2)
}
// SliceIntersect returns slice that are present in all the slice1 and slice2.
func SliceIntersect(slice1, slice2 []interface{}) (diffslice []interface{}) {
return utils.SliceIntersect(slice1, slice2)
}
// SliceChunk separates one slice to some sized slice.
func SliceChunk(slice []interface{}, size int) (chunkslice [][]interface{}) {
return utils.SliceChunk(slice, size)
}
// SliceRange generates a new slice from begin to end with step duration of int64 number.
func SliceRange(start, end, step int64) (intslice []int64) {
return utils.SliceRange(start, end, step)
}
// SlicePad prepends size number of val into slice.
func SlicePad(slice []interface{}, size int, val interface{}) []interface{} {
return utils.SlicePad(slice, size, val)
}
// SliceUnique cleans repeated values in slice.
func SliceUnique(slice []interface{}) (uniqueslice []interface{}) {
return utils.SliceUnique(slice)
}
// SliceShuffle shuffles a slice.
func SliceShuffle(slice []interface{}) []interface{} {
return utils.SliceShuffle(slice)
}