Skip to content

Instantly share code, notes, and snippets.

@begeric
Last active August 29, 2015 14:12
Show Gist options
  • Save begeric/00535a972782bbbf998c to your computer and use it in GitHub Desktop.
Save begeric/00535a972782bbbf998c to your computer and use it in GitHub Desktop.

Code used for the the following examples.

val tree = 
    q"""
       val a = 5
       val c = 3
       c = 5
       if (3 == 17) {
        val c = 1
       }
       else 2
       5
       """

Definition

def collect[C[_]] = new {
      def apply[A](f: PartialFunction[T, A]): C[A] //or List[A] if C[_] is infered to Nothing
}

Example

val l1 = tree.collect[Set]{case Dec.Val(_,a,_,_) => a.head.toString}
l1: Set[String] = Set(a, c)
val l2 = tree.collect{case Lit.Int(a) => a}
l2: List[Int] = List(5, 3, 5, 3, 17, 1, 2, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment