Your
removingFirst()
is not necessary. Set has afirst
and adropFirst()
that you can use to get those values.The typical Swift developer would expect to see this algorithm as an extension on Set rather than a free function.
This is a power set algorithm with the empty set subtracted. It would probably be better to just implement powerSet and then subtract the empty set. A google search will find a few implementations of powerSet. Here's my favorite:
extension Collection {
var powerSet: [[Element]] {
guard !isEmpty else { return [[]] }
return Array(dropFirst()).powerSet.flatMap { [$0, [self.first!] + $0] }
}
}
let result = Set(
[1, 2, 3]
.powerSet
.map { Set($0) }
)
.subtracting(Set([Set()]))
print(result)