regex - last element of array matching scala -


good afternoon! i'm using scala , want match first 3 element of list , last one, no matter how of them in list.

val mylist:list[list[int]] = list(list(3,1,2,3,4),list(23,45,6,7,2),list(3,3,2,1,5,34,43,2),list(8,5,3,34,4,5,3,2),list(3,2,45,56))  def parse(lists: list[int]): list[int] = lists.toarray match{   case array(item, site, buyer, _*, date) => list(item, site, buyer, date)}  mylist.map(parse _) 

but : error: bad use of _* (a sequence pattern must last pattern) understand why it, how can avoid?

my use case i'm reading hdfs, , every file has exact n (n constant , equal files) columns, want match of them, without writing case array(item1, item2 , ..., itemn) => list(item1, item2, itemk, itemn)

thank you!

you not need convert lists arrays, because lists designed pattern matching.

scala> mylist match {    case item :: site :: buyer :: tail if tail.nonempty =>      item :: site :: buyer :: list(tail.last) } res3: list[list[int]] = list(list(3, 1, 2, 3, 4), list(23, 45, 6, 7, 2),    list(3, 3, 2, 1, 5, 34, 43, 2), list(3, 2, 45, 56)) 

or more concise solution suggested kolmar

scala> mylist match {    case item :: site :: buyer :: (_ :+ date) => list(item, site, buyer, date)  } 

Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -