Scala filter on a list by index

前端 未结 6 1354
一整个雨季
一整个雨季 2021-02-15 12:52

I wanted to write it functionally, and the best I could do was:

list.zipWithIndex.filter((tt:Tuple2[Thing,Int])=>(tt._2%3==0)).unzip._1

to g

6条回答
  •  南笙
    南笙 (楼主)
    2021-02-15 13:18

    Not much clear, but still:

    xs.indices.collect { case i if i % 3 == 0 => xs(i) }
    

提交回复
热议问题