zipWith (mapping over multiple Seq) in Scala

前端 未结 6 742
無奈伤痛
無奈伤痛 2020-12-05 06:38

Suppose I have

val foo : Seq[Double] = ...
val bar : Seq[Double] = ...

and I wish to produce a seq where the baz(i) = foo(i) + bar(i). One

6条回答
  •  执念已碎
    2020-12-05 07:18

    In Scala 2.8:

    val baz = (foo, bar).zipped map (_ + _)
    

    And it works for more than two operands in the same way. I.e. you could then follow this up with:

    (foo, bar, baz).zipped map (_ * _ * _)
    

提交回复
热议问题