-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfind.go
139 lines (114 loc) · 5.41 KB
/
find.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
package ranges
// Find advances a range until cb(a, b) returns `true`, comparing with needle.
func Find[T any](haystack InputRange[T], cb func(a, b T) bool, needle T) InputRange[T] {
return DropWhile(haystack, func(a T) bool { return !cb(a, needle) })
}
// FindF is `Find` where the range can be saved.
func FindF[T any](haystack ForwardRange[T], cb func(a, b T) bool, needle T) ForwardRange[T] {
return DropWhileF(haystack, func(a T) bool { return !cb(a, needle) })
}
// FindS is `FindF` accepting a slice.
func FindS[T any](haystack []T, cb func(a, b T) bool, needle T) ForwardRange[T] {
return FindF(SliceRange(haystack), cb, needle)
}
// FindComparable advances a range until Front() == needle
func FindComparable[T comparable](haystack InputRange[T], needle T) InputRange[T] {
return Find(haystack, Eq[T], needle)
}
// FindComparableF is `FindComparable` where the range can be saved.
func FindComparableF[T comparable](haystack ForwardRange[T], needle T) ForwardRange[T] {
return FindF(haystack, Eq[T], needle)
}
// FindComparableS is `FindComparableF` accepting a slice.
func FindComparableS[T comparable](haystack []T, needle T) ForwardRange[T] {
return FindComparableF(SliceRange(haystack), needle)
}
// FindEqual advances a range until cb(a, b) returns `true` for all elments of `needle`.
func FindEqual[T any](haystack ForwardRange[T], cb func(a, b T) bool, needle ForwardRange[T]) ForwardRange[T] {
return DropWhileF(haystack, func(a T) bool { return !StartsWith[T](haystack.Save(), needle.Save(), cb) })
}
// FindEqualS is FindEqual accepting a slice.
func FindEqualS[T any](haystack []T, cb func(a, b T) bool, needle ForwardRange[T]) ForwardRange[T] {
return FindEqual(SliceRange(haystack), cb, needle)
}
// FindEqualComparable advances a range until `a == b` is satisifed for all elements of a `needle`.
func FindEqualComparable[T comparable](haystack ForwardRange[T], needle ForwardRange[T]) ForwardRange[T] {
return DropWhileF(
haystack,
func(a T) bool {
return !StartsWith[T](
haystack.Save(),
needle.Save(),
func(a, b T) bool { return a == b },
)
},
)
}
// FindEqualComparableS is FindEqualComparable accepting a slice.
func FindEqualComparableS[T comparable](haystack []T, needle ForwardRange[T]) ForwardRange[T] {
return FindEqualComparable(SliceRange(haystack), needle)
}
// FindAdjacent advances a range until cb(a, b) returns `true` for two adjacent elements.
func FindAdjacent[T any](haystack ForwardRange[T], cb func(a, b T) bool) ForwardRange[T] {
return DropWhileF(haystack, func(a T) bool {
saved := haystack.Save()
saved.PopFront()
return saved.Empty() || !cb(a, saved.Front())
})
}
// FindAdjacentS is FindAdjacent accepting a slice.
func FindAdjacentS[T any](haystack []T, cb func(a, b T) bool) ForwardRange[T] {
return FindAdjacent(SliceRange(haystack), cb)
}
// FindAdjacentComparable advances a range until a == b for two adjacent elements.
func FindAdjacentComparable[T comparable](haystack ForwardRange[T]) ForwardRange[T] {
return FindAdjacent(haystack, Eq[T])
}
// FindAdjacentComparableS is FindAdjacentComparable accepting a slice.
func FindAdjacentComparableS[T comparable](haystack []T) ForwardRange[T] {
return FindAdjacentComparable(SliceRange(haystack))
}
// FindAmong advances until `cb(a, b) == true` for any element of `needle`.
func FindAmong[T any](haystack InputRange[T], cb func(a, b T) bool, needle ForwardRange[T]) InputRange[T] {
return DropWhile(haystack, func(a T) bool {
return !Any(needle.Save(), func(b T) bool { return cb(a, b) })
})
}
// FindAmongF is FindAmong where the range can be saved.
func FindAmongF[T any](haystack ForwardRange[T], cb func(a, b T) bool, needle ForwardRange[T]) ForwardRange[T] {
return DropWhileF(haystack, func(a T) bool {
return !Any(needle.Save(), func(b T) bool { return cb(a, b) })
})
}
// FindAmongS is FindAmongF accepting a slice.
func FindAmongS[T any](haystack []T, cb func(a, b T) bool, needle ForwardRange[T]) ForwardRange[T] {
return FindAmongF(SliceRange(haystack), cb, needle)
}
// FindAmongComparable advances until `a == b` for any element of `needle`.
func FindAmongComparable[T comparable](haystack InputRange[T], needle ForwardRange[T]) InputRange[T] {
return FindAmong(haystack, Eq[T], needle)
}
// FindAmongComparableF is FindAmongComparable where the range can be saved.
func FindAmongComparableF[T comparable](haystack ForwardRange[T], needle ForwardRange[T]) ForwardRange[T] {
return FindAmongF(haystack, Eq[T], needle)
}
// FindAmongComparableS is FindAmongComparableF accepting a slice.
func FindAmongComparableS[T comparable](haystack []T, needle ForwardRange[T]) ForwardRange[T] {
return FindAmongComparableF(SliceRange(haystack), needle)
}
// CanFind returns `true` if `cb(a, b) == true`, comparing with needle.
func CanFind[T any](haystack InputRange[T], cb func(a, b T) bool, needle T) bool {
return Any(haystack, func(a T) bool { return cb(a, needle) })
}
// CanFindS is `CanFind` accepting a slice.
func CanFindS[T any](haystack []T, cb func(a, b T) bool, needle T) bool {
return CanFind[T](SliceRange(haystack), cb, needle)
}
// CanFindComparable returns `true` if `a == b`, comparing with needle.
func CanFindComparable[T comparable](haystack InputRange[T], needle T) bool {
return Any(haystack, func(a T) bool { return a == needle })
}
// CanFindComparableS is CanFindComparable accepting a slice.
func CanFindComparableS[T comparable](haystack []T, needle T) bool {
return CanFindComparable[T](SliceRange(haystack), needle)
}