This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
int64.go
155 lines (127 loc) · 3.62 KB
/
int64.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
// Code generated by github.com/schigh/slice/base/base.go. DO NOT EDIT.
package slice
import "sort"
// Int64 is the alias of []int64
type Int64 []int64
// Value returns the aliased []int64
func (slc Int64) Value() []int64 {
return []int64(slc)
}
// IndexOf returns the first index of needle, or -1 if not found
func (slc Int64) IndexOf(needle int64) int {
for i := 0; i < len(slc); i++ {
if needle == slc[i] {
return i
}
}
return NotInSlice
}
// Contains returns true if the slice contains needle
func (slc Int64) Contains(needle int64) bool {
return slc.IndexOf(needle) != NotInSlice
}
// SortAsc will sort the slice in ascending order
func (slc Int64) SortAsc() Int64 {
sort.SliceStable(slc, func(i, j int) bool {
return slc[i] < slc[j]
})
return slc
}
// SortDesc will sort the slice in descending order
func (slc Int64) SortDesc() Int64 {
sort.SliceStable(slc, func(i, j int) bool {
return slc[j] < slc[i]
})
return slc
}
// Reverse will reverse the order of the slice
func (slc Int64) Reverse() Int64 {
l := len(slc)
for i, j := 0, l-1; i < l/2; i++ {
slc[i], slc[j] = slc[j], slc[i]
j--
}
return slc
}
// Unique filters out duplicate int64 values
func (slc Int64) Unique() Int64 {
u := make([]int64, 0, len(slc))
m := make(map[int64]bool)
for i := 0; i < len(slc); i++ {
if _, ok := m[slc[i]]; !ok {
m[slc[i]] = true
u = append(u, slc[i])
}
}
return Int64(u)
}
// Filter will return all int64 values that evaluate true in the user-supplied function
func (slc Int64) Filter(f func(int64) bool) Int64 {
out := make([]int64, 0, len(slc))
for i := 0; i < len(slc); i++ {
if f(slc[i]) {
out = append(out, slc[i])
}
}
return Int64(out)
}
// Each will apply a function to each int64 in the slice.
// This function will iterate over the slice completely. No
// items in the slice should be mutated by this operation.
func (slc Int64) Each(f func(int64)) {
for i := 0; i < len(slc); i++ {
f(slc[i])
}
}
// TryEach will apply a function to each int64 in the slice.
// If the function returns an error, the iteration will stop and return
// the index of the element that caused the function to return the error.
// The second returned value will be first error returned from the
// supplied function, and nil otherwise.
// No items in the slice should be mutated by this operation.
func (slc Int64) TryEach(f func(int64) error) (int, error) {
for i := 0; i < len(slc); i++ {
if err := f(slc[i]); err != nil {
return i, err
}
}
return NotInSlice, nil
}
// IfEach will apply a function to each int64 in the slice.
// If the function returns false, the iteration will stop and return
// the index of the element that caused the function to return false.
// The second returned value will be true if all members of the slice
// cause the provided function to return true, and false otherwise.
// No items in the slice should be mutated by this operation.
func (slc Int64) IfEach(f func(int64) bool) (int, bool) {
for i := 0; i < len(slc); i++ {
if !f(slc[i]) {
return i, false
}
}
return NotInSlice, true
}
// Map will apply a function to each int64 in the slice and replace the previous value
func (slc Int64) Map(f func(int64) int64) {
for i := 0; i < len(slc); i++ {
slc[i] = f(slc[i])
}
}
// Chunk will divide the slice of int64 into smaller slicify defined by chunk length
func (slc Int64) Chunk(size int) [][]int64 {
l := len(slc)
if l == 0 || size <= 0 {
return make([][]int64, 0)
}
floor := l / size
out := make([][]int64, 0, floor+1)
var k int
for i := 0; i < floor; i++ {
k = i*size + size
out = append(out, slc[i*size:k])
}
if l > k {
out = append(out, slc[k:])
}
return out
}