-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.go
55 lines (48 loc) · 1.26 KB
/
select.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
package glinq
type (
SelectEnumerableIterator[T any, T2 any] struct {
iterator[T2]
source IEnumerable[T]
selector func(T) T2
enumerator IEnumerator[T]
}
)
func NewSelectIterator[T any, T2 any](source IEnumerable[T], selector func(T) T2) *SelectEnumerableIterator[T, T2] {
result := &SelectEnumerableIterator[T, T2]{
source: source,
selector: selector,
}
result.child = result
return result
}
func Select[T any, T2 any](source IEnumerable[T], selector func(T) T2) IEnumerable[T2] {
return NewSelectIterator(source, selector)
}
func (me *SelectEnumerableIterator[T, T2]) MoveNext() error {
switch me.state {
case 1:
me.enumerator = me.source.GetEnumerator()
me.state = 2
fallthrough
case 2:
err := me.enumerator.MoveNext()
if err != nil {
return err
}
item := me.enumerator.Current()
me.current = me.selector(item)
return nil
}
return ErrInvalidState
}
func (me *SelectEnumerableIterator[T, T2]) Clone() IEnumerator[T2] {
result := NewSelectIterator(me.source, me.selector)
result.state = 1
return result
}
func (me *SelectEnumerableIterator[T, T2]) Any() bool {
return me.source.Any()
}
func (me *SelectEnumerableIterator[T, T2]) Count() int {
return me.source.Count()
}