Created
January 31, 2020 07:09
-
-
Save linearhw/bd43efa8966e8cbfdf3afb1469cc606d to your computer and use it in GitHub Desktop.
This file contains 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 Promises | |
/// Check whether the values fulfill condition, and group the results. | |
/// - parameters: | |
/// - values: Values to check | |
/// - condition: Promise which states the condition | |
/// - returns: Promise which has grouped result of condition-passed values | |
public func filter<T>(values: [T], condition: ((T) -> Promise<Bool>)) -> Promise<[T]> { | |
let promises = values.map { value in condition(value).validate { $0 }.then { _ in value } } | |
return any(promises).then { $0.compactMap({ $0.value }) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment