-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselect.go
More file actions
56 lines (51 loc) · 1.31 KB
/
select.go
File metadata and controls
56 lines (51 loc) · 1.31 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
package ysq
// Select 将序列中的每个元素投影到新表单
func Select[T, TResult any](q *Query[T], selector func(T) TResult) *Query[TResult] {
qr := &Query[TResult]{
Next: func() Iterator[TResult] {
next := q.Next()
return func() (item TResult, ok bool) {
var tmp T
if tmp, ok = next(); ok {
item = selector(tmp)
}
return
}
},
}
return qr
}
func selectManyIterator[T, TResult any](q *Query[T], _ *Query[TResult],
selector func(T) *Query[TResult]) func() Iterator[TResult] {
return func() Iterator[TResult] {
outernext := q.Next()
var inner *T
var innernext Iterator[TResult]
return func() (item TResult, ok bool) {
var tmp T
for !ok {
if inner == nil {
if tmp, ok = outernext(); !ok {
return
}
inner = &tmp
innernext = selector(*inner).Next()
}
if item, ok = innernext(); !ok {
inner = nil
}
}
return
}
}
}
// SelectMany 将序列的每个元素投影并将结果序列合并为一个序列
func SelectMany[T, TResult any](q *Query[T], selector func(T) *Query[TResult]) *Query[TResult] {
qr := Query[TResult]{}
qr.Next = selectManyIterator(q, &qr, selector)
return &qr
}
// Select 将序列中的每个元素投影到新表单
func (q *Query[T]) Select(selector func(T) T) *Query[T] {
return Select(q, selector)
}