-
-
Save indyfromoz/46b6b35136300e20b0af129140f64a65 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
extension Collection { | |
func firstMap<T>(_ transform: @escaping (Element) -> T?) -> T? { | |
for element in self { | |
if let transformed = transform(element) { | |
return transformed | |
} | |
} | |
return nil | |
} | |
} | |
let values = ["a", "b", "3"] | |
let result = values.firstMap { string in | |
return Int(string) | |
} | |
print(result) // Optional(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment