Skip to content

Instantly share code, notes, and snippets.

@kailuowang
Last active August 29, 2015 14:26
Show Gist options
  • Save kailuowang/c73a70c9194e662163e8 to your computer and use it in GitHub Desktop.
Save kailuowang/c73a70c9194e662163e8 to your computer and use it in GitHub Desktop.
Functional Asyn Example
def createOrUpdateCard( profileId: ProfileId, contextName: ContextName): FutureEither[CardId] = {
lazy val nowF = getAndValidateGoogleNowCredential(profileId)
lazy val contentF = getContentFromStationService(profileId, contextName)
lazy val contextF = ensureContextExistsOnGoogle(contextName)
def createCard = {
for {
now <- nowF
content <- contentF
context <- contextF
toCreate = new Card().setContent(content)
created <- BlockingIO.futureEither(createCardOnGoogle(toCreate, now))
} yield created.getCardId
}
def updateCard = {
val existingCard =
for {
now <- nowF
context <- contextF
card <- findCardOnGoogle(profileId, context)))
} yield card
for {
now <- nowF
card <- existingCard
content <- contentF
toUpdate = card.setContent(content)
updated <- BlockingIO.futureEither(updateCard(toUpdate, now))
updatedId <- updated.getCardId
} yield updatedId
}
updateCard orElse createCard
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment