Skip to content

Instantly share code, notes, and snippets.

@iMostfa
Created August 20, 2020 19:48
Show Gist options
  • Save iMostfa/af1c328c3383c67f3a5fc1ffd04435f1 to your computer and use it in GitHub Desktop.
Save iMostfa/af1c328c3383c67f3a5fc1ffd04435f1 to your computer and use it in GitHub Desktop.
flatMap without losing the value from upstream
import Combine
var moviessPublisher = ["James bond","abo elaraby","F.R.I.E.N.D.S","House Of Cards"]
.publisher
.eraseToAnyPublisher()
func rating(for movie: String) -> AnyPublisher<Int,Never> { //could be a restful api that takes movie name and return the rating
return Just((movie.count * 2) % 5).eraseToAnyPublisher()
}
//somewhere else where we want to load movies, and also, get rating for each movie
moviessPublisher
.flatMap { (movieName) in
return rating(for: movieName)
.zip(Just(movieName))
}
.sink { (data) in
print("movie name is \(data.1) and movie rating is \(data.0)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment