Skip to content

Instantly share code, notes, and snippets.

@laevandus
Last active March 1, 2021 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laevandus/9ef8d5e97e87e90121be7fbda436ddc9 to your computer and use it in GitHub Desktop.
Save laevandus/9ef8d5e97e87e90121be7fbda436ddc9 to your computer and use it in GitHub Desktop.
// 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