Skip to content

Instantly share code, notes, and snippets.

@laevandus

laevandus/Collection.swift Secret

Last active Mar 1, 2021
Embed
What would you like to do?
// Swift Foundation
func prefix(while predicate: (Self.Element) throws -> Bool) rethrows -> Self.SubSequence
func map<T>(_ transform: (Self.Element) throws -> T) rethrows -> [T]
// prefixMap (new)
extension Collection {
@inlinable func prefixMap<T>(_ transform: (Self.Element) throws -> T?) rethrows -> [T] {
var result = [T]()
for element in self {
if let transformedElement = try transform(element) {
result.append(transformedElement)
}
else {
break
}
}
return result
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment