-
Notifications
You must be signed in to change notification settings - Fork 0
/
xtract.go
156 lines (141 loc) · 3.49 KB
/
xtract.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
145
146
147
148
149
150
151
152
153
154
155
156
package xtract
import "iter"
// compatibility checks
var (
_ Extractor[int, string] = (*SliceExtractor[string])(nil)
_ Extractor[string, int] = (*MapExtractor[string, int])(nil)
)
// Extractor provides features to extract values from a collection.
type Extractor[K comparable, V any] interface {
// ByValue filters the values of the collection by their values.
ByValue(condition func(V) bool) Extractor[K, V]
// ByKey filters the values of the collection by their keys.
ByKey(condition func(K) bool) Extractor[K, V]
// ByKeyAndValue filters the values of the collection by their keys and values.
ByKeyAndValue(condition func(K, V) bool) Extractor[K, V]
// Values returns a sequence of values.
Values() iter.Seq[V]
}
// SliceExtractor is implementation of Extractor for slice.
type SliceExtractor[V any] struct {
seq iter.Seq2[int, V]
}
// ByValue See: Extractor.ByValue
func (x *SliceExtractor[V]) ByValue(condition func(V) bool) Extractor[int, V] {
return &SliceExtractor[V]{
seq: func(yield func(int, V) bool) {
for i, v := range x.seq {
if condition(v) && !yield(i, v) {
return
}
}
},
}
}
// ByKey See: Extractor.ByKey
func (x *SliceExtractor[V]) ByKey(condition func(int) bool) Extractor[int, V] {
return &SliceExtractor[V]{
seq: func(yield func(int, V) bool) {
for i, v := range x.seq {
if condition(i) && !yield(i, v) {
return
}
}
},
}
}
// ByKeyAndValue See: Extractor.ByKeyAndValue
func (x *SliceExtractor[V]) ByKeyAndValue(condition func(int, V) bool) Extractor[int, V] {
return &SliceExtractor[V]{
seq: func(yield func(int, V) bool) {
for i, v := range x.seq {
if condition(i, v) && !yield(i, v) {
return
}
}
},
}
}
// Values See: Extractor.Values
func (x *SliceExtractor[V]) Values() iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range x.seq {
if !yield(v) {
return
}
}
}
}
// FromSlice returns Extractor for a slice.
func FromSlice[V any](s []V) Extractor[int, V] {
return &SliceExtractor[V]{
seq: func(yield func(int, V) bool) {
for i, v := range s {
if !yield(i, v) {
return
}
}
}}
}
// MapExtractor is implementation of Extractor for map.
type MapExtractor[K comparable, V any] struct {
seq iter.Seq2[K, V]
}
// ByValue See: Extractor.ByValue
func (x MapExtractor[K, V]) ByValue(condition func(V) bool) Extractor[K, V] {
return &MapExtractor[K, V]{
seq: func(yield func(K, V) bool) {
for k, v := range x.seq {
if condition(v) && !yield(k, v) {
return
}
}
},
}
}
// ByKey See: Extractor.ByKey
func (x MapExtractor[K, V]) ByKey(condition func(K) bool) Extractor[K, V] {
return &MapExtractor[K, V]{
seq: func(yield func(K, V) bool) {
for k, v := range x.seq {
if condition(k) && !yield(k, v) {
return
}
}
},
}
}
// ByKeyAndValue See: Extractor.ByKeyAndValue
func (x MapExtractor[K, V]) ByKeyAndValue(condition func(K, V) bool) Extractor[K, V] {
return &MapExtractor[K, V]{
seq: func(yield func(K, V) bool) {
for k, v := range x.seq {
if condition(k, v) && !yield(k, v) {
return
}
}
},
}
}
// Values See: Extractor.Values
func (x MapExtractor[K, V]) Values() iter.Seq[V] {
return func(yield func(V) bool) {
for _, v := range x.seq {
if !yield(v) {
return
}
}
}
}
// FromMap returns Extractor for a map.
func FromMap[K comparable, V any](m map[K]V) Extractor[K, V] {
return &MapExtractor[K, V]{
seq: func(yield func(K, V) bool) {
for k, v := range m {
if !yield(k, v) {
return
}
}
},
}
}