Skip to content

Instantly share code, notes, and snippets.

@izqui
Created December 23, 2014 11:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save izqui/62f11cb52e63f9818e93 to your computer and use it in GitHub Desktop.
Save izqui/62f11cb52e63f9818e93 to your computer and use it in GitHub Desktop.
Swift Async functions
func mapParallel <A, B> (values: [A], task: ( (A, (B) -> () ) -> () ), callback:([B]) -> ()) {
let tasks: [((B) -> ()) -> ()] = values.map({
v in
return {
cb in
task(v, cb)
}
})
return parallel(tasks, callback)
}
func parallel <A> (tasks: [((A) -> ()) -> ()], callback:([A]) -> ()) {
var arr = Array<A?>(count: tasks.count, repeatedValue: nil)
var add: (Int, A) -> () = {
i, v in
arr[i] = v
var newArr = [A]()
for a in arr {
if a != nil {
newArr.append(a!)
} else {
return
}
}
callback(newArr)
}
for (i, t) in enumerate(tasks) {
t({a in add(i, a)})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment