Guest User

Untitled

a guest
Feb 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.65 KB | None | 0 0
  1. package cenas
  2.  
  3. object Payload {
  4.   def unapply(z: List[Int]): Option[List[Int]] = if (z.length == z.head + 1) Option(z tail) else None
  5. }
  6.  
  7. object EndToken {
  8.   def unapply(z: List[Int]): Option[List[Int]] = z.reverse match {
  9.     case 0x0D :: 0x0A :: tail => Option(tail.reverse)
  10.     case _ => None
  11.   }
  12. }
  13.  
  14. object Message {
  15.   def unapply(z: List[Int]): Option[List[Int]] = z match {
  16.     case 0xFC :: EndToken(x) => Option(x)
  17.     case _ => None
  18.   }
  19. }
  20.  
  21. object Main extends App {
  22.   val x = List(0xFC, 0x03, 0x01, 0x02, 0x03, 0x0A, 0x0D)
  23.  
  24.   x match {
  25.     case Message(Payload(payload)) => println (payload)
  26.     case _ => println("No match")
  27.   }
  28. }
Add Comment
Please, Sign In to add comment