-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.go
More file actions
64 lines (51 loc) · 1.54 KB
/
transform.go
File metadata and controls
64 lines (51 loc) · 1.54 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
package weaklinq
//----------------------------------------------------------------------------//
// Transform //
//----------------------------------------------------------------------------//
////////////////////////////////////////////////////////////////////////////////
// GetThese returns a new Iterable where the items are transformed by selector.
func (iterable Iterable[T]) GetThese(selector func(T) any) Iterable[any] {
return Iterable[any]{
Seq: func(yield func(any) bool) {
iterable.Seq(func(item T) bool {
return yield(selector(item))
})
},
}
/*
linq.From([]T{...}).
GetThese(
func(item T) any {
return item.ItemField
},
)
*/
}
////////////////////////////////////////////////////////////////////////////////
// Get returns a new Iterable where the items are transformed by fieldName.
// If T is not a struct, or fieldName is not found, this function will panic.
func (iterable Iterable[T]) Get(fieldName string) Iterable[any] {
return iterable.GetThese(
getFieldNameFunc[T](fieldName),
)
/*
linq.From([]T{...}).
Get("ItemField")
*/
}
////////////////////////////////////////////////////////////////////////////////
// AsAny converts an Iterable of any type T to an Iterable of type any.
func (iterable Iterable[T]) AsAny() Iterable[any] {
return Iterable[any]{
Seq: func(yield func(any) bool) {
iterable.Seq(func(item T) bool {
return yield(item)
})
},
}
/*
linq.AsAnyIterable(
linq.From([]T{...}),
)
*/
}