Scala How to Get a Subscription by Index

I have List (1, 2, 3, 4, 5) and am trying to get subscriptions: List (3, 4) from it like this:

List(1 ,2 ,3 ,4 ,5).slice(this.size - 3 , this.size - 1 ) 

But I got an error message

 error: value size is not a member of object 

How can I use the "this" parameter in Scala, like in Java. Are there other ways to achieve the goal. Thank you very much.

+6
source share
4 answers

You must declare the list first and then access the list using its name list , not this :

 val list = List(1 ,2 ,3 ,4 ,5) list.slice(list.size -3, list.size -1) 

If you really want to do this on one line, use reverse , but this is not very efficient:

 List(1 ,2 ,3 ,4 ,5).reverse.slice(1, 3).reverse 

By the way, this code is also invalid in Java. this refers to the surrounding object, not the list.

+5
source

If you want to use the last 3 or n elements, you can use takeRight(3) or takeRight(n) .

Edit based on question editing:

If you do not need to take the first f and last l elements, then

 List(1,2,3,....n).drop(f).dropRight(l) 

In your case, it will be List(1,2,3,4,5).drop(2).dropRight(1)

+3
source

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

0
source

You have a list (1, 2, 3, 4, 5)

1) Is it a function or object or class?

2) You want to use the return values ​​from the list (x, x, 3, 4, x)

3) you need to use the size outside the parentheses to return the value from your new List function ...

4) create a function to complete this step, then return it to a variable ...

5) when starting the List () function, use list variables (A, B, C, D, E)

6) all objects must be fully declared before use. For example, how can you use basketball without air inside? or How can you use football without pork skin? or How can you use a computer without a processor? all components of the objects must be fully declared and work correctly in programming!

7) All data, functions, objects inside your List () function should work correctly or ... work.,

8) You said: "In general, I also want to get some elements in the middle."

9) As a veteran programmer ... you need to know exactly where the elements in your List () function are

10) Also, from the very beginning, before all these functions that you want to β€œuse”, you must load classes that are populated with all the functions that you use at the head of your program.

11) you cannot use a function the same way you use an object or class!

12) Finally, you just want to be creative when a programming language sets rules or boundaries ... Scala is not Java ...

Does it help? with "error: value size is not a member of the object"

-2
source

Source: https://habr.com/ru/post/987193/


All Articles