Iterate over scala list
11,285
Solution 1
Here's one way.
scala> List("a","b","c","d","e")
res0: List[String] = List(a, b, c, d, e)
scala> res0.splitAt(res0.size/2)
res1: (List[String], List[String]) = (List(a, b),List(c, d, e))
scala> res1._1.foreach(println(_))
a
b
scala> res1._2.foreach(println(_))
c
d
e
Solution 2
Use sliding to create iterators,
scala> val input = List(1, 2, 3)
input: List[Int] = List(1, 2, 3)
scala> val step = (input.length + 1 ) / 2
step: Int = 2
scala> val sliding = input.sliding(step, step)
sliding: Iterator[List[Int]] = non-empty iterator
scala> val left = sliding.next()
left: List[Int] = List(1, 2)
scala> val right = sliding.next()
right: List[Int] = List(3)
scala> left.foreach(println)
1
2
scala> right.foreach(println)
3
or use take & drop,
scala> val input = List(1, 2, 3)
input: List[Int] = List(1, 2, 3)
scala> val step = (input.length + 1 ) / 2
step: Int = 2
scala> input.take(step).foreach(println)
1
2
scala> input.drop(step).foreach(println)
3
Author by
user2694377
Updated on June 04, 2022Comments
-
user2694377 almost 2 years
I have a list of strings in scala template of play framework.
I want to iterate over half of list at one time and then the other half of list on the second time.
I am not sure how to write efficient iterator for this.I have tried
@for(i <- 0 until list.length/2 ) {list(i) }
and then for second loop@for(i <- list.length/2+1 until list.length ) { list(i) }
This works but complexity becomes high.Then later I did
@defining(list.size) { size => @for(i <- 0 until size/2) {list(i) } }
Now it seems to work fine.-
Jeremy D over 10 yearsPlease, edit your question above, even if you had what seems to be a good answer.
-
user2694377 over 10 yearsI just tried @for(i <- 0 until list.length/2 ) {list(i) } and then for second loop @for(i <- list.length/2+1 until list.length ) { list(i) } . This seems to be working fine for now.
-
-
user2694377 over 10 yearsYeah. This would work but I dont want the overhead of creating another list.I just found the syntax in for loop to iterate over only the half of list . @for(i <- 0 until list.length/2 ) {} and then from list.length/2+1 until list.length.
-
senia over 10 years@user2694377: the cost of creating 2 new collections is
O(N)
, iterating throughList
using index isO(N^2)
. It's really not a good idea. You could use anIterator
withdrop
andtake
if you don't want to copy collection. -
user2694377 over 10 yearsOkay. Thanks. Sorry I dint notice that it was O(n^2). Thanks for pointing it out.