scalaunapply

Does Scala allow for this kind of extractor?


Let's say I have this collection:

val a = Array(Array(1,2,3,4,5),Array(4,5),Array(5),Array(1,2,6,7,8))

Is there a way to define an extractor which would work in the following way:

a.foreach(e => {
   e match {
      case Array( ending with 5 ) => 
      case _ =>
   }
})

Sorry for the pseudocode, but I don't know how to express it. Is there a way to match something having 5 as the last element? What if I would want to match something having a 1 as the first element and a 5 as the last? Could this work for arrays of various lengths ( note that I specifically chose different lengths for my arrays in the example ).

Thanks!


Solution

  • a.foreach(e => {
       e match {
          case a: Array[Int] if a.last == 5 => 
          case _ =>
       }
    })
    

    You can do something a little better for matching on the first elements:

    a.foreach(e => {
       e match {
          case Array(1, _*) => 
          case _ => 
       }
    })
    

    Unfortunately the @_* thing has to be the last item in the list of array arguments. But you can make the matching before that as complex as you want.

    scala> val Array(1, x @_*) = Array(1,2,3,4,5)
    x: Seq[Int] = Vector(2, 3, 4, 5)
    
    scala> val Array(1, b, 3, x @_*) = Array(1,2,3,4,5)
    b: Int = 2
    x: Seq[Int] = Vector(4, 5)