-
Notifications
You must be signed in to change notification settings - Fork 0
/
to_set.go
98 lines (93 loc) · 2.22 KB
/
to_set.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
// Copyright 2022 streams Author. All Rights Reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package streams
// ToSet function
//
// T: element type
//
// R: result type
//
// toSetFunc: the to set function
func ToSet[T any, R comparable](ts []T, toSetFunc func(e T) R) Set[R] {
s := NewSet[R]()
for _, t := range ts {
s.Add(toSetFunc(t))
}
return s
}
// ToSetThenMap function
//
// T: element type
//
// R: result type
//
// RR: map type
//
// toSetFunc: the to map function
//
// mapFunc: the map function
func ToSetThenMap[T any, R comparable, RR any](ts []T, toSetFunc func(t T) R, mapFunc func(r R) RR) []RR {
rs := make([]RR, 0)
s := NewSet[R]()
for _, t := range ts {
s.Add(toSetFunc(t))
}
s.Iterate(func(r R) {
rs = append(rs, mapFunc(r))
})
return rs
}
// ToSet2 function
//
// T: element type
//
// R1: the set1 type
//
// R2: the set2 type
//
// toSetFunc1: the to set1 function
//
// toSetFunc2: the to set2 function
func ToSet2[T any, R1, R2 comparable](ts []T, toSetFunc1 func(e T) R1, toSetFunc2 func(e T) R2) (Set[R1], Set[R2]) {
s1 := NewSet[R1]()
s2 := NewSet[R2]()
for _, t := range ts {
s1.Add(toSetFunc1(t))
s2.Add(toSetFunc2(t))
}
return s1, s2
}
// ToSet3 function
//
// T: element type
//
// R1: the set1 type
//
// R2: the set2 type
//
// R3: the set3 type
//
// toSetFunc1: the to set1 function
//
// toSetFunc2: the to set2 function
//
// toSetFunc3: the to set3 function
func ToSet3[T any, R1, R2, R3 comparable](ts []T, toSetFunc1 func(t T) R1, toSetFunc2 func(t T) R2, toSetFunc3 func(t T) R3) (Set[R1], Set[R2], Set[R3]) {
s1 := NewSet[R1]()
s2 := NewSet[R2]()
s3 := NewSet[R3]()
for _, t := range ts {
s1.Add(toSetFunc1(t))
s2.Add(toSetFunc2(t))
s3.Add(toSetFunc3(t))
}
return s1, s2, s3
}