I am doing a pattern matching on a list. Is there anyway I can access the first and last element of the list to compare?
I want to do something like..
case List(x, _*, y) if(x == y) => true
or
case x :: _* :: y =>
or something similar…
where x and y are first and last elements of the list..
How can I do that.. any Ideas?
Use the standard :+ and +: extractors from the
scala.collectionpackageORIGINAL ANSWER
Define a custom extractor object.
Can be used as:
(For your case:
case x :: (_ :+ y) if(x == y) => true)