Last active
November 23, 2015 06:35
-
-
Save mkhl/40989cac8e33522d4fd2 to your computer and use it in GitHub Desktop.
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
import PromiseKit | |
extension CollectionType { | |
func traverse<T>(transform: Generator.Element -> Promise<T>) -> Promise<[T]> { | |
return Promise { fulfill, reject in | |
var accumulator: [T] = [] | |
var generator = self.generate() | |
func recurse() { | |
guard let next = generator.next() else { | |
fulfill(accumulator) | |
return | |
} | |
transform(next).then { result -> () in | |
accumulator.append(result) | |
recurse() | |
} | |
} | |
recurse() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment