Skip to main content
deleted 7 characters in body
Source Link
Daniel T.
  • 866
  • 5
  • 16
  • Your removingFirst() is not necessary. Set has a first and a dropFirst() 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)
  • Your removingFirst() is not necessary. Set has a first and a dropFirst() 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)
  • Your removingFirst() is not necessary. Set has a first and a dropFirst() that you can use to get those values.

  • The typical Swift developer would expect to see this algorithm as an extension 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)
Source Link
Daniel T.
  • 866
  • 5
  • 16

  • Your removingFirst() is not necessary. Set has a first and a dropFirst() 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)