Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Skip first N elements in scala iterable

Tags:

I'd like to know if scala includes a way to skip the first N elements of an iterable, so that for instance

(1 to 5).WHATIWANT(3).foreach(println(_)) 

would print only 4 and 5.

I understand there's slice, but if the length of the sequence can't be obtained in advance, like in my case, that's not gonna do.

Ideas?

like image 473
em70 Avatar asked Mar 01 '12 10:03

em70


People also ask

How to remove the first element of a list in Scala?

Coming to list, tail() method is used to skip the first element of the list.

How to define iterator in Scala?

It provide us hasNext() and next() method to work with collection elements. The basic syntax to define the iterate in scala is as follows; valiterate_name = Iterator(value1, value2, value3, so on ....) In this way, we can define an iterate in scala.


1 Answers

(1 to 5).drop(3).foreach(println(_)) 
like image 100
Eastsun Avatar answered Dec 06 '22 07:12

Eastsun