Skip to content

Instantly share code, notes, and snippets.

@linearhw
Created January 31, 2020 07:09
Show Gist options
  • Save linearhw/bd43efa8966e8cbfdf3afb1469cc606d to your computer and use it in GitHub Desktop.
Save linearhw/bd43efa8966e8cbfdf3afb1469cc606d to your computer and use it in GitHub Desktop.
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