How to get the element index when mapping an array in Scala?

前端 未结 3 2070
时光说笑
时光说笑 2020-12-14 01:42

Let\'s consider a simple mapping example:


  val a = Array(\"One\", \"Two\", \"Three\")
  val b = a.map(s => myFn(s))

What I need is to use

3条回答
  •  情歌与酒
    2020-12-14 02:03

    What about this? I think it should be fast and it's pretty. But I'm no expert on Scala speed...

    a.foldLeft(0) ((i, x) => {myFn(x, i); i + 1;} )
    

提交回复
热议问题