generated from dogmatiq/template-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
first.go
70 lines (59 loc) · 1.82 KB
/
first.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
package linger
import "time"
// First returns the first of its arguments for which the predicate
// function p returns true.
//
// If the p returns false for all values, v is the zero-value and ok is false.
func First(p DurationPredicate, values ...time.Duration) (v time.Duration, ok bool) {
for _, v := range values {
if p(v) {
return v, true
}
}
return 0, false
}
// MustFirst returns the first of its arguments for which the predicate
// function p returns true.
//
// It panics if p returns false for all values.
func MustFirst(p DurationPredicate, values ...time.Duration) time.Duration {
if t, ok := First(p, values...); ok {
return t
}
panic("the predicate did not match any input values")
}
// FirstT returns the first of its arguments for which the predicate
// function p returns true.
//
// If the p returns false for all values, v is the zero-value and ok is false.
func FirstT(p TimePredicate, values ...time.Time) (v time.Time, ok bool) {
for _, v := range values {
if p(v) {
return v, true
}
}
return time.Time{}, false
}
// MustFirstT returns the first of its arguments for which the predicate
// function p returns true.
//
// It panics if p returns false for all values.
func MustFirstT(p TimePredicate, values ...time.Time) time.Time {
if t, ok := FirstT(p, values...); ok {
return t
}
panic("the predicate did not match any input values")
}
// Defaulter returns a DurationTransform that falls back to the first value for
// which the predicate function p returns true.
//
// The transform input value is checked first, then each of the given values in
// order. It panics if p returns false for all values.
func Defaulter(p DurationPredicate, values ...time.Duration) DurationTransform {
return func(v time.Duration) time.Duration {
if p(v) {
return v
}
return MustFirst(p, values...)
}
}