-
Notifications
You must be signed in to change notification settings - Fork 225
/
select.go
109 lines (96 loc) · 3.78 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
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
package linq
// Select projects each element of a collection into a new form. Returns a query
// with the result of invoking the transform function on each element of
// original source.
//
// This projection method requires the transform function, selector, to produce
// one value for each value in the source collection. If selector returns a
// value that is itself a collection, it is up to the consumer to traverse the
// subcollections manually. In such a situation, it might be better for your
// query to return a single coalesced collection of values. To achieve this, use
// the SelectMany method instead of Select. Although SelectMany works similarly
// to Select, it differs in that the transform function returns a collection
// that is then expanded by SelectMany before it is returned.
func (q Query) Select(selector func(interface{}) interface{}) Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
return func() (item interface{}, ok bool) {
var it interface{}
it, ok = next()
if ok {
item = selector(it)
}
return
}
},
}
}
// SelectT is the typed version of Select.
// - selectorFn is of type "func(TSource)TResult"
// NOTE: Select has better performance than SelectT.
func (q Query) SelectT(selectorFn interface{}) Query {
selectGenericFunc, err := newGenericFunc(
"SelectT", "selectorFn", selectorFn,
simpleParamValidator(newElemTypeSlice(new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
selectorFunc := func(item interface{}) interface{} {
return selectGenericFunc.Call(item)
}
return q.Select(selectorFunc)
}
// SelectIndexed projects each element of a collection into a new form by
// incorporating the element's index. Returns a query with the result of
// invoking the transform function on each element of original source.
//
// The first argument to selector represents the zero-based index of that
// element in the source collection. This can be useful if the elements are in a
// known order and you want to do something with an element at a particular
// index, for example. It can also be useful if you want to retrieve the index
// of one or more elements. The second argument to selector represents the
// element to process.
//
// This projection method requires the transform function, selector, to produce
// one value for each value in the source collection. If selector returns a
// value that is itself a collection, it is up to the consumer to traverse the
// subcollections manually. In such a situation, it might be better for your
// query to return a single coalesced collection of values. To achieve this, use
// the SelectMany method instead of Select. Although SelectMany works similarly
// to Select, it differs in that the transform function returns a collection
// that is then expanded by SelectMany before it is returned.
func (q Query) SelectIndexed(selector func(int, interface{}) interface{}) Query {
return Query{
Iterate: func() Iterator {
next := q.Iterate()
index := 0
return func() (item interface{}, ok bool) {
var it interface{}
it, ok = next()
if ok {
item = selector(index, it)
index++
}
return
}
},
}
}
// SelectIndexedT is the typed version of SelectIndexed.
// - selectorFn is of type "func(int,TSource)TResult"
// NOTE: SelectIndexed has better performance than SelectIndexedT.
func (q Query) SelectIndexedT(selectorFn interface{}) Query {
selectGenericFunc, err := newGenericFunc(
"SelectIndexedT", "selectorFn", selectorFn,
simpleParamValidator(newElemTypeSlice(new(int), new(genericType)), newElemTypeSlice(new(genericType))),
)
if err != nil {
panic(err)
}
selectorFunc := func(index int, item interface{}) interface{} {
return selectGenericFunc.Call(index, item)
}
return q.SelectIndexed(selectorFunc)
}