Scala list pattern matching in value declaration -
assuming following list of 4 tuples:
> def getlist = list( (1,2), (3,4), (5,6), (7,8) ) i want parse 3 sections in new values declaration - the last tuple (l1,l2), the penultimate tuple (p1,p2) , the rest of list rest.
moreover, i'd pattern-match tuples well. example:
> val (rest :: (p1, p2) :: (l1, l2) ) = getlist rest = list( (1,2), (3,4) ) p1 = 5 p2 = 6 l1 = 7 l2 = 8 the expected output not work because :: expects list right-side argument. tried several different possibilities such ::: list wrapper. however, haven't managed achieve goal.
is possible? don't want reverse list - instead, i'm looking elegant solution applicable value declaration (thus no match please). hope in one-line solution.
edit: scala code runner version 2.9.2
you use :+ matcher object:
val rest :+ ((p1, p2)) :+ ((l1, l2)) = getlist it's the part of standard library since scala 2.10
for previous version redefine using source
Comments
Post a Comment