-
Notifications
You must be signed in to change notification settings - Fork 3
/
int8.go
96 lines (93 loc) · 1.85 KB
/
int8.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
package main
import "errors"
type Int8Slice []int8
func (r Int8Slice) FirstOrDefault(f func(int8) bool) int8 {
for _, slEl := range r {
if f(slEl) {
return slEl
}
}
var defVal int8
return defVal
}
func (r Int8Slice) First(f func(int8) bool) (int8, error) {
for _, slEl := range r {
if f(slEl) {
return slEl, nil
}
}
var defVal int8
return defVal, errors.New("Not found")
}
func (r Int8Slice) Where(f func(int8) bool) []int8 {
res := make([]int8, 0)
for _, slEl := range r {
if f(slEl) {
res = append(res, slEl)
}
}
return res
}
func (r Int8Slice) Select(f func(int8) interface{}) []interface{} {
res := make([]interface{}, len(r))
for i := range r {
res[i] = f(r[i])
}
return res
}
func (r Int8Slice) Page(number int64, perPage int64) ([]int8, error) {
if number <= 0 {
return nil, errors.New("Page number should start with 1")
}
number--
first := number * perPage
if first > int64(len(r)) {
return []int8{}, nil
}
last := first + perPage
if last > int64(len(r)) {
last = int64(len(r))
}
return r[first:last], nil
}
func (r Int8Slice) Any(f func(int8) bool) bool {
_, err := r.First(f)
return err == nil
}
func (r Int8Slice) Contains(el int8) (bool, error) {
for _, slEl := range r {
if slEl == el {
return true, nil
}
}
return false, nil
}
func (r Int8Slice) GetUnion(sl2 []int8) ([]int8, error) {
result := make([]int8, 0)
for _, sl1El := range r {
for _, sl2El := range sl2 {
areEqual := sl1El == sl2El
if areEqual {
result = append(result, sl1El)
}
}
}
return result, nil
}
func (r Int8Slice) InFirstOnly(sl2 []int8) ([]int8, error) {
result := make([]int8, 0)
for _, sl1El := range r {
found := false
for _, sl2El := range sl2 {
areEqual := sl1El == sl2El
if areEqual {
found = true
continue
}
}
if !found {
result = append(result, sl1El)
}
}
return result, nil
}