-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiltering.go
More file actions
78 lines (63 loc) · 1.78 KB
/
filtering.go
File metadata and controls
78 lines (63 loc) · 1.78 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
package weaklinq
//----------------------------------------------------------------------------//
// Filtering //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
// FilterOnThis returns a new Iterable where the items are filtered by the given
// predicate.
func (iterable Iterable[T]) FilterOnThis(predicate func(T) bool) Iterable[T] {
return Iterable[T]{
Seq: func(yield func(T) bool) {
iterable.Seq(func(item T) bool {
if predicate(item) {
return yield(item)
}
return true
})
},
}
/*
linq.From([]T{...}).
FilterOnThis(
func(item T) bool {
return boolExpression
},
)
*/
}
////////////////////////////////////////////////////////////////////////////////
// FilterOn returns a new Iterable where the items are filtered by the given
// fieldName. If T is not a struct, or fieldName is not found, this function
// will panic.
func (iterable Iterable[T]) FilterOn(fieldName string) Iterable[T] {
nameFunc := getFieldNameFunc[T](fieldName)
return iterable.FilterOnThis(
func(item T) bool {
return nameFunc(item).(bool)
},
)
/*
linq.From([]T{...}).
FilterOn("BoolFieldName")
*/
}
////////////////////////////////////////////////////////////////////////////////
// Distinct returns a new Iterable where the items are distinct.
func (iterable Iterable[T]) Distinct() Iterable[T] {
seen := make(map[any]bool)
return Iterable[T]{
Seq: func(yield func(T) bool) {
iterable.Seq(func(item T) bool {
if _, ok := seen[item]; !ok {
seen[item] = true
return yield(item)
}
return true
})
},
}
/*
linq.From([]T{...}).
Distinct()
*/
}