Skip to content

Instantly share code, notes, and snippets.

@chriseidhof
Last active September 25, 2015 12:44
Show Gist options
  • Save chriseidhof/71f79b6c29be8a58fe3d to your computer and use it in GitHub Desktop.
Save chriseidhof/71f79b6c29be8a58fe3d to your computer and use it in GitHub Desktop.
no-escape.swift
extension Array {
func parallelMap<U>(transform: Element -> U) -> [U] {
var result: [U?] = Array<U?>(count: count, repeatedValue: nil)
dispatch_apply(count, dispatch_get_global_queue(0, 0)) {
result[$0] = transform(self[$0])
}
return result.map { $0! }
}
}
extension Array {
func parallelMap<U>(@noescape transform: Element -> U) -> [U] {
typealias Transform = Element -> U
let transform1 = unsafeBitCast(transform, Transform.self)
var result: [U?] = Array<U?>(count: count, repeatedValue: nil)
dispatch_apply(count, dispatch_get_global_queue(0, 0)) { x in
result[x] = transform1(self[x])
}
return result.map { $0! }
}
}
@chrisfsampaio
Copy link

You could use return result.flatMap { $0 } to unwrap optionals and avoid using the bang.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment