Skip to content

Instantly share code, notes, and snippets.

@taisukeoe
Last active August 29, 2015 14:25
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 taisukeoe/3e36a3c6a3bc45be0c01 to your computer and use it in GitHub Desktop.
Save taisukeoe/3e36a3c6a3bc45be0c01 to your computer and use it in GitHub Desktop.
Break down Callback which has two ore more functions into the same number of PartialFunction
trait Callback{
def onSet(s:Set[String]):Unit
def onList(l:List[String]):Unit
}
def funcWithCallback(s:String,callback:Callback):Unit = ???
def funcAsFuture(s:String):Future[Traversable[String]] = {
val p = Promise[Traversable[String]]()
val f = p.future
funcWithCallback(s,new Callback{
def onSet(s:Set[String]):Unit = p.success(s)
def onList(l:List[String]):Unit = p.success(l)
})
}
val pf1:PartialFunction[Set[String],Result] = ...
val pf2:PartialFunction[List[String],Result] = ...
//If PartialFucntion is Monoid, might it be appendable as follows?
val pf:PartialFunction[Traversable[String],Result] = pf1 append pf2
//Break down Callback into two ore more PartialFunction implementations
val f:Future[Result] = for{
trav <- funcAsFuture.map(s)
} pf(trav)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment