Skip to content

Instantly share code, notes, and snippets.

@brentsimmons
Created August 16, 2015 04:59
Show Gist options
  • Save brentsimmons/c794cb207aaeb1291348 to your computer and use it in GitHub Desktop.
Save brentsimmons/c794cb207aaeb1291348 to your computer and use it in GitHub Desktop.
//: Playground - noun: a place where people can play
import Cocoa
var aSet = Set<Int>()
aSet.insert(1)
aSet.insert(2)
aSet.insert(3)
// The rest of this code doesn't even remotely work.
typealias TestBlock = (obj: AnyObject) -> Bool
extension Set {
func anyObjectPassingTest(testBlock: TestBlock) -> AnyObject? {
for oneObject in self {
if testBlock(oneObject) {
return oneObject
}
}
return nil
}
}
let x = aSet.anyObjectPassingTest { (obj) -> Bool in
return obj % 2 == 0
}
print(x)
@GuyEnglish
Copy link

@natecook1000: The second solution is pretty nifty. Finds the first element. I assume it stops iterating the collection there. Then returns it. There's (from a casual understanding) an extra pointer deference there for the return self[index] but who cares and that's a nice way of expressing it. You're totally right that it should match the indexOf method. I'd missed @NoEscape at some point. DIdn't know that was a thing! Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment