Last active
September 25, 2015 12:44
-
-
Save chriseidhof/71f79b6c29be8a58fe3d to your computer and use it in GitHub Desktop.
no-escape.swift
This file contains hidden or 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
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! } | |
} | |
} |
This file contains hidden or 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
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! } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You could use
return result.flatMap { $0 }
to unwrap optionals and avoid using the bang.