score:6
Accepted answer
note that the documentation on takewhile
states:
reuse: after calling this method, one should discard the iterator it was called on, and use only the iterator that was returned. using the old iterator is undefined, subject to change, and may result in changes to the new iterator as well.
so you shouldn't use i
, after calling takewhile
on it.
but to achieve what you want you can use the span
method:
scala> val i = (1 to 8).iterator
i: iterator[int] = non-empty iterator
scala> val (onetothree, rest) = i.span(_ <= 3)
onetothree: iterator[int] = non-empty iterator
rest: iterator[int] = unknown-if-empty iterator
scala> onetothree.tolist
res1: list[int] = list(1, 2, 3)
scala> val fourtosix = rest.takewhile(_ <= 6)
fourtosix: iterator[int] = non-empty iterator
scala> fourtosix.tolist
res2: list[int] = list(4, 5, 6)
Source: stackoverflow.com
Related Query
- Scala Iterator.takeWhile is dropping the failed element
- takeWhile: also need first element failed the condition in scala
- Appending an element to the end of a list in Scala
- Finding the index of an element in a list scala
- Scala - Iterator over all the lines in the files in a directory
- all but the last item from a Scala Iterator (a.k.a. Iterator.init)
- Accessing the next element in list to compare in Scala
- What is the current element in a Scala DoubleLinkedList?
- Dropping multiple columns from Spark dataframe by Iterating through the columns from a Scala List of Column names
- How can I idiomatically "remove" a single element from a list in Scala and close the gap?
- How to use takeWhile with an Iterator in Scala
- Match to the n-th element of a list in Scala
- How to find the last occurrence of an element in a Scala List?
- Transform only the first element of a Scala list
- Getting the element from a 1-element Scala collection
- How to get a scala Map value, when the key is a tuple in which only the first element is known?
- Moving an element to the front of a list in Scala
- Retrieving largest indices of the bounded element in a multidimensional array in Scala
- Downloading Metals (Vscode Scala extension) failed with the following: spawn C:\Program Files (x86)\Java\jdk1.8.0_144\bin\bin\java ENOENT
- Transform only the last element of a Scala list
- Scala XML extract the element in comma separator
- What is the preferred way to get the first element of a random-access Scala collection
- Compare each element of Scala list against rest of elements in the list
- Transform a Scala Stream to a new Stream which is the sum of the current element and the previous element
- Scala - Find the exact index of the current element of a List
- How to do this in Scala way: get the first element from Option[Seq[String]]
- scala create new list by omitting the n element from existing list with tuples
- How could I get the first K element in scala Array?
- Idiomatic to find the first element in a collection that matches a given sub-type in Scala
- Scala how to repeat the first element of Array until Array.size reach a certain number
More Query from same tag
- Can Actors in Scala fail to process messages? (example in O'Reilly's Programming Scala)
- How to log None errors in a for-yield block
- how to avoid while loop and return in scala
- Scala Slick 3.0.1 Relationship to self
- How to use Sum on groupBy result in Spark DatFrames?
- Is possible for a worker to be a coordinator and send to other workers in Spark?
- how to compile scala and java files to together in netbeans IDE?
- In Scala, how to refactor codes with Option class like this?
- Scala cast to generic type
- How to filter elements in List where predicate is over entire other List
- Which JVM language doesn't have type erasure?
- Throw error on null value in spark column
- Sending Streaming Data as JSON in Java/Scala
- spark sql dataframe join with renaming in a loop
- Scala Newbie Inquiry on Generalizing Parameter Datatype
- Read files recursively in scala
- Improving MVP in Scala
- remove pipe delimiter from data using spark
- Enum.Value vs Enum#Value
- How to getQueueUrl as an array with an array of queueName?
- Race condition caused by mvar
- Is there a way to prohibit delimiting by ', ' and just delimit the csv file with ',' in scala
- Scala Interpreter Blocks
- How to correct java.io.FileNotFoundException: (No such file or directory) in scala?
- Companion object for Set in Scala
- How to build a Play2 project using play-yeoman from IntelliJ IDEA
- mxnet scala use one_hot params error
- Scala Cannot resolve symbol AnalysisException
- Scala Routing Error
- How to call a scala function with parameters using java classloader?