datatypes: add a Set.array/0 method to help get all the elements from a set as an array (#24206)

This commit is contained in:
kbkpbot 2025-04-14 00:25:32 +08:00 committed by GitHub
parent 8a39132b6e
commit 880a9873a4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 0 deletions

View File

@ -128,3 +128,8 @@ pub fn (l Set[T]) subset(r Set[T]) bool {
}
return true
}
// array returns an array representation of the set
pub fn (l Set[T]) array() []T {
return l.elements.keys()
}

View File

@ -131,3 +131,9 @@ fn test_subset() {
subset.add_all(['b', 'c'])
assert set.subset(subset)
}
fn test_array() {
mut set := Set[string]{}
set.add_all(['a', 'b', 'c'])
assert set.array().len == 3
}