Arnold deVos
@a4dev
adv-contact-trick@langdale.com.au
case class Song( n: Int, thing: String)
val s = Song( 10, "green bottles")
val Song(10, x) = s
val y = s match { case Song(10, thing) => thing }
val z = for( Song(10, thing) <- Some(s) ) yield thing
val pf: PartialFunction[Song, String] = {
case Song(10, thing) => thing
case Song(_, thing) => thing + ", but not 10 of them"
}
val a = pf(s)
object ExtractCount {
def unapply(s: Song) = Some(s.n)
}
val b = s match { case ExtractCount(n) => n + " things" }
object countOption extends (Song => Option[Int]) {
def apply(s: Song) = Some(s.n)
}
val c = countOption(s) map { _ + " things" } getOrElse { throw new MatchError(s) }
class Extractor[A,B]( f: A => Option[B] ) {
def unapply( a: A ) = f(a)
}
def pattern[B](pf: PartialFunction[Name,B]) =
new Extractor(pf.lift)