Created
December 8, 2017 15:38
-
-
Save jnd71/612c09c6a62585d05d4281bb0d9f3d6c to your computer and use it in GitHub Desktop.
Scala Programming Taste
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Which of these is a better style? Is there much of a difference? | |
def createMicrosoftAudio0(utterance: Utterance, svc: MsSynthSvc = MicrosoftSpeech.Synthesize): Future[SynthesizedSpeech] = { | |
if(!utterance.text.isDefined) return Future.exception(new UtteranceTextNotFound(utterance)) | |
val req = SynthesizeRequest(utterance.convo.id, utterance.text.get) | |
for { | |
resp <- svc(req) | |
} yield SynthesizedSpeed(resp._1, resp._2) | |
} | |
def createMicrosoftAudio1(utterance: Utterance, svc: MsSynthSvc = MicrosoftSpeech.Synthesize): Future[SynthesizedSpeech] = { | |
if(utterance.emptyText) return Future.exception(new UtteranceTextNotFound(utterance)) | |
val req = SynthesizeRequest.fromUtterance(utterance) | |
svc(req).map(SynthesizedSpeech) | |
} |
Twitter informs me the return should be on a newline, and I agree:
def createMicrosoftAudio1(utterance: Utterance, svc: MsSynthSvc = MicrosoftSpeech.Synthesize): Future[SynthesizedSpeech] = {
if(utterance.emptyText)
return Future.exception(new UtteranceTextNotFound(utterance))
val req = SynthesizeRequest.fromUtterance(utterance)
svc(req).map(SynthesizedSpeech)
}
For this type of mapping, I really like custom extractors as a matter of taste.
object NonEmpty {
def unapply(utterance: Utterance): Option[String] = utterance.text match {
case result @ Some(text) if text.nonEmpty => result
case _ => None
}
}
which then allows us to pattern match:
def createMicrosoftAudio0(utterance: Utterance, svc: MsSynthSvc = MicrosoftSpeech.Synthesize): Future[SynthesizedSpeech] = {
utterance match {
case NonEmpty(text) =>
svc( SynthesizeRequest(utterance.convo.id, text) ) map SythesizedSpeech
case _ =>
Future.exception(new UtteranceTextNotFound(utterance))
}
I'd place that NonEmpty
inside the Utterance
object so it'd pattern match like case Utterance.NonEmpty(text) =>
Given the generalised Speech client façade I've created, direct "extractors" would prolly be nice:
object Utterance {
…
object Recognised {
def unapply(utterance: Utterance): Option[SpeechRequest] = utterance.text match {
case Some(text) if text.nonEmpty => Some(SpeechRequest(utterance.convo.id, text))
case _ => None
}
}
object Unrecognised {
def unapply(utterance: Utterance): Option[UtteranceTextNotFound] = utterance.text match {
case Some(text) if text.isEmpty => Some(UtteranceTextNotFound(utterance))
case None => Some(UtteranceTextNotFound(utterance))
case _ => None
}
}
and then
def createAudio(utterance: Utterance, svc: SpeechClient): Future[SpeechResponse] = utterance match {
case Utterance.Recognised(request) => svc(request)
case Utterance.Unrecognised(error) => Future.exception(error)
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
These are both functionally equivalent given some additional implementation for
createMicrosoftAudio1