-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathranger_types.go
More file actions
59 lines (53 loc) · 2.28 KB
/
ranger_types.go
File metadata and controls
59 lines (53 loc) · 2.28 KB
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
package arrayr
import "github.com/eenti-utils/typr"
type directionalRanger[V any] func(rf typr.Ranger[V], a []V)
// directives and flags passed to the Range function
type RangeOpts[V any] struct {
// a non-zero int value, where
// - positive values imply ranging in order, from index 0 to n
// - negative values imply ranging in order, from index n to 0
// the value 1 (default) means handle each element, starting at index 0
//
// the value 2 means handle every second element, starting at index 0
Step int
// a function that returns bool true, when the given element
// should be handled by the user-defined Ranger function, and bool false, otherwise
//
// filtered elements are not passed on to the user-defined Ranger function
FilterElements typr.Qualifier[V]
// a function that returns nil, unless the given element is not considered valid, otherwise
// a non-nil error value is returned
//
// elements deemed to be invalid are not passed on to the user-defined Ranger function
ValidateElements typr.Validator[V]
// when set to true, stops ranging when the first "invalid" element is encountered
// - this option is only effective when a viable ValidateElements function has been defined
HaltOnInvalidElements bool
// ⚠ Use With Caution! ⚠
//
// when set to true:
// - the arrayr.Range(...) function (as usual) blocks, until all elements are finished processing
// - all eligible elements are submitted to the user-defined Ranger function and processed concurrently
// - the return value of the user-defined Ranger function (eg. typr.Break) is ignored
// - elements do not necessarily finish processing in order of submission
// - ValidateElements function behaves as a FilterElements function, and HaltOnInvalidElements
// is ignored, if set
// ⚠ User-defined Ranger functions should generally take care to operate in a concurrent-safe
// manner, when using this option ⚠
Concurrently bool
}
func newRangeOpts[V any]() RangeOpts[V] {
return RangeOpts[V]{Step: 1}
}
func (o *RangeOpts[V]) assumeValues(uOpts ...RangeOpts[V]) {
if len(uOpts) == 0 {
return
}
userOpts := uOpts[0]
if userOpts.Step != 0 {
o.Step = userOpts.Step
}
o.FilterElements = userOpts.FilterElements
o.ValidateElements = userOpts.ValidateElements
o.Concurrently = userOpts.Concurrently
}