@@ -75,9 +75,10 @@ func (s Set[T]) IsEmpty() bool {
7575 return len (s ) == 0
7676}
7777
78- // Clear removes all elements from the set.
79- func (s Set [T ]) Clear () {
78+ // Clear removes all elements from the set. Modifies the set in place. Returns the set itself.
79+ func (s Set [T ]) Clear () Set [ T ] {
8080 clear (s )
81+ return s
8182}
8283
8384// Clone returns a copy of the set.
@@ -107,32 +108,36 @@ func (s Set[T]) Add(element T) bool {
107108 return true
108109}
109110
110- // AddMany adds the elements to the set.
111- func (s Set [T ]) AddMany (elements ... T ) {
111+ // AddMany adds the elements to the set. Modifies the set in place. Returns the set itself.
112+ func (s Set [T ]) AddMany (elements ... T ) Set [ T ] {
112113 for _ , e := range elements {
113114 s [e ] = struct {}{}
114115 }
116+ return s
115117}
116118
117- // AddSlice adds the elements of the slice to the set.
118- func (s Set [T ]) AddSlice (source []T ) {
119+ // AddSlice adds the elements of the slice to the set. Modifies the set in place. Returns the set itself.
120+ func (s Set [T ]) AddSlice (source []T ) Set [ T ] {
119121 for _ , e := range source {
120122 s [e ] = struct {}{}
121123 }
124+ return s
122125}
123126
124- // AddSeq adds the elements of the sequence to the set.
125- func (s Set [T ]) AddSeq (source iter.Seq [T ]) {
127+ // AddSeq adds the elements of the sequence to the set. Modifies the set in place. Returns the set itself.
128+ func (s Set [T ]) AddSeq (source iter.Seq [T ]) Set [ T ] {
126129 for e := range source {
127130 s [e ] = struct {}{}
128131 }
132+ return s
129133}
130134
131- // AddSet adds the elements of the other set to the set.
132- func (s Set [T ]) AddSet (source Set [T ]) {
135+ // AddSet adds the elements of the other set to the set. Modifies the set in place. Returns the set itself.
136+ func (s Set [T ]) AddSet (source Set [T ]) Set [ T ] {
133137 for e := range source {
134138 s [e ] = struct {}{}
135139 }
140+ return s
136141}
137142
138143// Remove removes the element from the set. Returns true if the element was removed. Returns false if the element was not in the set.
@@ -144,32 +149,36 @@ func (s Set[T]) Remove(element T) bool {
144149 return true
145150}
146151
147- // RemoveMany removes the elements from the set.
148- func (s Set [T ]) RemoveMany (elements ... T ) {
152+ // RemoveMany removes the elements from the set. Modifies the set in place. Returns the set itself.
153+ func (s Set [T ]) RemoveMany (elements ... T ) Set [ T ] {
149154 for _ , e := range elements {
150155 delete (s , e )
151156 }
157+ return s
152158}
153159
154- // RemoveSlice removes the elements of the slice from the set.
155- func (s Set [T ]) RemoveSlice (source []T ) {
160+ // RemoveSlice removes the elements of the slice from the set. Modifies the set in place. Returns the set itself.
161+ func (s Set [T ]) RemoveSlice (source []T ) Set [ T ] {
156162 for _ , e := range source {
157163 delete (s , e )
158164 }
165+ return s
159166}
160167
161- // RemoveSeq removes the elements of the sequence from the set.
162- func (s Set [T ]) RemoveSeq (source iter.Seq [T ]) {
168+ // RemoveSeq removes the elements of the sequence from the set. Modifies the set in place. Returns the set itself.
169+ func (s Set [T ]) RemoveSeq (source iter.Seq [T ]) Set [ T ] {
163170 for e := range source {
164171 delete (s , e )
165172 }
173+ return s
166174}
167175
168- // RemoveSet removes the elements of the other set from the set.
169- func (s Set [T ]) RemoveSet (source Set [T ]) {
176+ // RemoveSet removes the elements of the other set from the set. Modifies the set in place. Returns the set itself.
177+ func (s Set [T ]) RemoveSet (source Set [T ]) Set [ T ] {
170178 for e := range source {
171179 delete (s , e )
172180 }
181+ return s
173182}
174183
175184// Contains returns true if the set contains all elements.
0 commit comments