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