-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpredicates.go
More file actions
93 lines (82 loc) · 1.85 KB
/
predicates.go
File metadata and controls
93 lines (82 loc) · 1.85 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
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
package got
import (
"iter"
)
// True returns a predicate that always returns true.
func True[T any](T) bool {
return true
}
// False returns a predicate that always returns false.
func False[T any](T) bool {
return false
}
// Not returns a predicate that negates the result of the given predicate.
func Not[T any](f func(T) bool) func(T) bool {
return func(v T) bool {
return !f(v)
}
}
// And returns a predicate that returns true if all predicates return true.
func And[T any](preds ...func(T) bool) func(T) bool {
return func(v T) bool {
for _, f := range preds {
if !f(v) {
return false
}
}
return true
}
}
// Or returns a predicate that returns true if any of the predicates return true.
func Or[T any](preds ...func(T) bool) func(T) bool {
return func(v T) bool {
for _, f := range preds {
if f(v) {
return true
}
}
return false
}
}
// InContainer returns a predicate that checks if a value is in container.
func InContainer[T any](s Container[T]) func(T) bool {
return func(v T) bool {
return s.Contains(v)
}
}
// InSet returns a predicate that checks if a value is in a set.
func InSet[T comparable](s map[T]struct{}) func(T) bool {
return func(v T) bool {
_, ok := s[v]
return ok
}
}
// InMap returns a predicate that checks if a key is in a map.
func InMap[K comparable, V any](m map[K]V) func(K) bool {
return func(k K) bool {
_, ok := m[k]
return ok
}
}
// InSlice returns a predicate that checks if a value is in a slice.
func InSlice[T comparable](s []T) func(T) bool {
return func(v T) bool {
for _, e := range s {
if e == v {
return true
}
}
return false
}
}
// InSeq returns a predicate that checks if a value is in a sequence.
func InSeq[T comparable](s iter.Seq[T]) func(T) bool {
return func(v T) bool {
for i := range s {
if i == v {
return true
}
}
return false
}
}