Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.