Created
October 18, 2020 04:21
-
-
Save masakih/257c9109563c6fd0fa54ee7daaaea452 to your computer and use it in GitHub Desktop.
できた #CodePiece
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 Sequence { | |
| func traverse<T>(_ transform: @escaping (Element) -> AnyPublisher<T, Never>) -> AnyPublisher<[T], Never> { | |
| var index = 0 | |
| return self | |
| .publisher | |
| .flatMap { i -> AnyPublisher<(Int, T), Never> in | |
| defer { index += 1 } | |
| let index = index | |
| return Future<(Int, T), Never> { promise in | |
| DispatchQueue.global().async { | |
| let se = DispatchSemaphore(value: 1) | |
| DispatchQueue(label: "AAAA").async { | |
| _ = transform(i) | |
| .sink { t in | |
| promise(.success((index, t))) | |
| se.signal() | |
| } | |
| } | |
| se.wait() | |
| } | |
| } | |
| .eraseToAnyPublisher() | |
| } | |
| .collect() | |
| .map { $0.sorted { $0.0 < $1.0 } } | |
| .map { $0.map { $0.1 } } | |
| .eraseToAnyPublisher() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment