-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters.go
144 lines (128 loc) · 4.21 KB
/
filters.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
package arrayr
import (
"github.com/eenti-utils/typr"
)
// applies the specified Qualifier function to each of the specified elements
//
// each element, where the Qualifier function returns bool true is added to the array
// that is returned
func Filter[V any](ok typr.Qualifier[V], a ...V) (r []V) {
if ok == nil {
r = a
return
}
return doFilter(ok, a...)
}
// returns the first element, where the specified Qualifier function returns bool true, and
// - bool true, if an element was qualified
// - bool false, otherwise
func First[V any](ok typr.Qualifier[V], a ...V) (r V, f bool) {
return doFirst(ok, a...)
}
// returns the first element, where the specified Qualifier function returns bool true, or
// the specified default value, if no element was qualified
func FirstOr[V any](ok typr.Qualifier[V], defaultVal V, a ...V) (r V) {
r = defaultVal
if fv, exists := doFirst(ok, a...); exists {
r = fv
}
return
}
// returns the first element, where the specified Qualifier function returns bool true, or
// the zero value of the given type, if no element was qualified
func FirstOrZero[V any](ok typr.Qualifier[V], a ...V) (r V) {
if fv, exists := doFirst(ok, a...); exists {
r = fv
}
return
}
// returns the last element, where the specified Qualifier function returns bool true, and
// - bool true, if an element was qualified
// - bool false, otherwise
func Last[V any](ok typr.Qualifier[V], a ...V) (r V, f bool) {
return doLast(ok, a...)
}
// returns the last element, where the specified Qualifier function returns bool true, or
// the specified default value, if no element was qualified
func LastOr[V any](ok typr.Qualifier[V], defaultVal V, a ...V) (r V) {
r = defaultVal
if len(a) == 0 {
return
}
if fv, exists := doLast(ok, a...); exists {
r = fv
}
return
}
// returns the last element, where the specified Qualifier function returns bool true, or
// the zero value of the given type, if no element was qualified
func LastOrZero[V any](ok typr.Qualifier[V], a ...V) (r V) {
if len(a) == 0 {
return
}
if fv, exists := doLast(ok, a...); exists {
r = fv
}
return
}
// returns the element at the specified index, and a nil error, if the index is valid
// - valid non-negative index values range from 0 to n-1, where n is the number of elements in the array
// - valid negative index value range from -n to -1, where n is the number of elements in the array
// a negative index references an offset from the end of the array, such that
// - negative 1 (-1) references the last element
// - negative 2 (-1) references the next to last element
// and so forth
func Nth[V any](i int, a ...V) (r V, e error) {
return doNth(i, a...)
}
// returns the element at the specified index, if the index is valid, or
// the specified default value
//
// valid index values range from -n to n-1, where n is the number of elements in the array
//
// zero references the first element of the array
//
// a negative index references an offset from the end of the array, such that
// - negative 1 (-1) references the last element
// - negative 2 (-2) references the next to last element
// and so forth
func NthOr[V any](i int, defaultVal V, a ...V) (r V) {
r = defaultVal
if nth, err := doNth(i, a...); err == nil {
r = nth
}
return
}
// returns the element at the specified index, if the index is valid, or
// the zero value of the given type
//
// valid index values range from -n to n-1, where n is the number of elements in the array
//
// zero references the first element of the array
//
// a negative index references an offset from the end of the array, such that
// - negative 1 (-1) references the last element
// - negative 2 (-2) references the next to last element
// and so forth
func NthOrZero[V any](i int, a ...V) (r V) {
if nth, err := doNth(i, a...); err == nil {
r = nth
}
return
}
// returns an array comprised of all of the specified elements considered to be valid, by the specified Validator function
// - if no elements were considered valid, then the resulting array has a length of zero
func Valid[V any](validate typr.Validator[V], a ...V) (r []V) {
if validate == nil {
if len(a) > 0 {
r = a
}
return
}
for _, e := range a {
if err := validate(e); err == nil {
r = append(r, e)
}
}
return
}