I have a list of row identifiers representing DB records. I would like to download them from the database asynchronously, upload each record asynchronously to the remote server, and then, when everything is uploaded, record the identifiers of the downloaded records.
Since I am in Scala 2.9.2, I use the core-util Future kernel implementation, but it should work just like 2.10 futures in terms of monadic transformations.
The general concept is as follows:
def fetch(id: String): Future[Option[Record]] def upload(record: Record): Future[String] def notifyUploaded(ids: Seq[String]): Unit val ids: Seq[String] = ....
I try to do this with understanding, but the fact that fetch returns Future of Option makes it unclear and the code does not compile:
for { id <- ids maybeRecord <- fetch(id) record <- maybeRecord uploadedId <- upload(record) } yield uploadedId
Compiling the results results in the following error:
scala: type mismatch; found : com.twitter.util.Future[String] required: Option[?] uploadedId <- upload(record) ^
What am I missing? why does the compiler expect uploadedId to become an option? Is there any nice way I could get around this?
scala future monads
Electric monk
source share