Skip to content

Instantly share code, notes, and snippets.

@jasdev
Last active January 18, 2021 03:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasdev/cbc4aee0a184d6d4130697139799dad9 to your computer and use it in GitHub Desktop.
Save jasdev/cbc4aee0a184d6d4130697139799dad9 to your computer and use it in GitHub Desktop.
Tidied form of Publisher.prefixInclusive(while:).
extension Publisher {
func prefixInclusive(while predicate: @escaping (Output) -> Bool) -> AnyPublisher<Output, Failure> {
flatMap { value in
Just(PrefixInclusiveEvent.predicatePassingValueOrIncluded(value))
.append(!predicate(value) ? [.end] : [])
.setFailureType(to: Failure.self)
}
.prefix(while: \.isPredicatePassingValueOrIncluded)
.compactMap(\.output)
.eraseToAnyPublisher()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment