-
Notifications
You must be signed in to change notification settings - Fork 1
/
select.go
77 lines (68 loc) · 2.71 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
package flinx
// 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 Select[T, V any](selector func(T) V) func(q Query[T]) Query[V] {
return func(q Query[T]) Query[V] {
return Query[V]{
Iterate: func() Iterator[V] {
next := q.Iterate()
return func() (item V, ok bool) {
var it T
it, ok = next()
if ok {
item = selector(it)
}
return
}
},
}
}
}
// 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 SelectIndexed[T, V any](selector func(int, T) V) func(q Query[T]) Query[V] {
return func(q Query[T]) Query[V] {
return Query[V]{
Iterate: func() Iterator[V] {
next := q.Iterate()
index := 0
return func() (item V, ok bool) {
var it T
it, ok = next()
if ok {
item = selector(index, it)
index++
}
return
}
},
}
}
}