Created
July 30, 2020 15:57
-
-
Save tjeerdintveen/82888946cca62e7915acb8affdc5c233 to your computer and use it in GitHub Desktop.
This file contains 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
@escaping can be left out actually