Skip to content

Instantly share code, notes, and snippets.

@penland365
Created December 8, 2017 15:38
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 penland365/612c09c6a62585d05d4281bb0d9f3d6c to your computer and use it in GitHub Desktop.
Save penland365/612c09c6a62585d05d4281bb0d9f3d6c to your computer and use it in GitHub Desktop.
Scala Programming Taste
// 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)
}
@penland365
Copy link
Author

These are both functionally equivalent given some additional implementation for createMicrosoftAudio1

@penland365
Copy link
Author

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)
}

@kirked
Copy link

kirked commented Dec 8, 2017

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))
}

@kirked
Copy link

kirked commented Dec 8, 2017

I'd place that NonEmpty inside the Utterance object so it'd pattern match like case Utterance.NonEmpty(text) =>

@kirked
Copy link

kirked commented Dec 8, 2017

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