The problem is that this word does not indicate your list, if you want to take some parameter in the middle of using the list and take rigth, please read the Scala Docs at the end, this is the link
val my_lst = List(1 ,2 ,3 ,4 ,5) my_lst.takeRight(3).take(2) my_lst: List[Int] = List(1, 2, 3, 4, 5) res0: List[Int] = List(3, 4)
first takeRight with an index, counting my_lst.size on the right is the position, so you have to write the position, and then how many elements you want in your new sublist
Here are explanations of how the methods work in the Scala List
from Scala Help Doc
def slice (from: Int, to: Int): List [A]
returns
a list containing the elements greater than or equal to index from extending up to (but not including) index until of this list.
Definition classes List β LinearSeqOptimized β IterableLike β TraversableLike β GenTraversableLike Example:
// Given the list val letters = List ('a', 'b', 'c', 'd', 'e')
// slice returns all elements starting with the from index, and then, // up to the until index (excluding the until index.) letters.slice (1,3) // Returns list ('b', 'c')
in your case use length or size
scala> val my_lst = List(1 ,2 ,3 ,4 ,5) my_lst: List[Int] = List(1, 2, 3, 4, 5) scala> my_lst.slice(my_lst.length-3,my_lst.length) res0: List[Int] = List(3, 4, 5)
but actually the Scala List class has a lot of functions that can fit you. In your case, if you want the last 3 elements to use takeRight
def takeRight (n: Int): List [A]
Selects the last n items.
P
the number of elements to take returns a list consisting only of the last n elements of this list, or else the whole list, if it has less than n elements.
Definition Classes List β IterableLike
scala> my_lst.takeRight(3) res2: List[Int] = List(3, 4, 5)
Take a look at Scala Docs