Skip to content

Instantly share code, notes, and snippets.

@sam
Created September 28, 2015 18:00
Show Gist options
  • Save sam/7be14ce3f7632e97a8b7 to your computer and use it in GitHub Desktop.
Save sam/7be14ce3f7632e97a8b7 to your computer and use it in GitHub Desktop.
Trying to figure out how to chain partial functions with inheritance...
trait Parser { self: Actor =>
def parse: PartialFunction[Opened, Unit]
def receive: Receive = parse orElse(super.receive) andThen(_ => done())
def collectWork: Receive
def done(): Unit = {
context become collectWork
context.self ! Done
}
}
trait StoryListParser extends Parser { self: Actor =>
def parse = {
case Opened(url, content) if url.getPath.endsWith("stories/") =>
// ... PARSE LIST OF STORIES ...
}
}
trait StoryParser extends Parser { self: Actor =>
def parse = {
case Opened(url, content) if url.getPath.matches("""stories\/[^\/]+$""") =>
// ... PARSE INDIVIDUAL STORY ...
}
}
class Job extends Actor with StoryListParser with StoryParser {
def collectWork: Receive = {
case Done => context.parent ! Done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment