Skip to content

Instantly share code, notes, and snippets.

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 xuyazhong/1870a5ae1564f6202fc87de69ee97b04 to your computer and use it in GitHub Desktop.
Save xuyazhong/1870a5ae1564f6202fc87de69ee97b04 to your computer and use it in GitHub Desktop.
数组flatMap
https://github.com/apple/swift/blob/master/stdlib/public/core/SequenceAlgorithms.swift.gyb
@_inlineable
public func flatMap<SegmentOfResult : Sequence>(_ transform: (${GElement}) throws -> SegmentOfResult) rethrows -> [SegmentOfResult.${GElement}] {
var result: [SegmentOfResult.${GElement}] = []
for element in self {
result.append(contentsOf: try transform(element))
}
return result
}
@_inlineable
public func flatMap<ElementOfResult>(_ transform: (${GElement}) throws -> ElementOfResult?) rethrows -> [ElementOfResult] {
var result: [ElementOfResult] = []
for element in self {
if let newElement = try transform(element) {
result.append(newElement)
}
}
return result
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment