0

Old version of code before XCode 7 Beta 3:

extension Array {
    func filterByIndex<S: SequenceType where S.Generator.Element == Int>(indices: S) -> [T] {
        return Array(PermutationGenerator(elements: self, indices: indices))
    }

    func find(includedElement: T -> Bool) -> Int? {
        for (idx, element) in self.enumerate() {
            if includedElement(element) {
                return idx
            }
        }
        return nil
    }

}

New version of code after XCode 7 Beta 3:

extension Array {
    func filterByIndex<S: SequenceType where S.Generator.Element == Int>(indices: S) -> [Element] {
        return Array(PermutationGenerator(elements: self, indices: indices))
    }

    func find(includedElement: Element -> Bool) -> Int? {
        for (idx, element) in self.enumerate() {
            if includedElement(element) {
                return idx
            }
        }
        return nil
    }
}

But now the function filterByIndex gives me an error when I write this line:

let names = (namesArr as! [String]).filterByIndex(dupes)

'[String]' does not have a member named 'filterByIndex'

What is changed ?

Pang
  • 9,564
  • 146
  • 81
  • 122
Bogdan Bogdanov
  • 882
  • 11
  • 36
  • 79
  • I agree with @Arkku, the code seems to work just fine. I guess if you could give an example `namesArr` and `dupes` that might help narrow down the issue (and be sure to include the types for those). – justinpawela Jul 11 '15 at 05:26

1 Answers1

0

The new version of the code works fine for me with:

[ "zero", "one", "two", "three", "four" ].filterByIndex([1, 3])
// result: [ "one", "three" ]

I'm assuming the problem you're having is somewhere else. I initially suspected the type of dupes (the definition of which is not show) no matching the requirements of the generic function, but in my tests the error message should be different in that case.

Arkku
  • 41,011
  • 10
  • 62
  • 84
  • `let dupes = indicesOfUniques(namesArr as! [String])` `public func indicesOfUniques(source: [T]) -> [Int] { var seen: Set = [] return source.indices.filter { if seen.contains(source[$0]) { return false } else { seen.insert(source[$0]) return true } } }` I changed T with Element in indicesOfUniques and doesn't working... – Bogdan Bogdanov Jul 11 '15 at 10:29
  • @BogdanBogdanov Did you try the code from my answer? If that works for you, then the question is wrong and the issue is with `dupes` or `namesArr`. To see which, replace each one of them with a manually created array of in turn. – Arkku Jul 11 '15 at 12:52