'map'保留了元素的数量,因此在元组上使用它似乎是明智的.
到目前为止我的尝试:
scala> (3,4).map(_*2)
error: value map is not a member of (Int, Int)
(3,4).map(_*2)
^
scala> (3,4).productIterator.map(_*2)
error: value * is not a member of Any
(3,4).productIterator.map(_*2)
^
scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2)
res4: Iterator[Int] = non-empty iterator
scala> (3,4).productIterator.map(_.asInstanceOf[Int]*2).toList
res5: List[Int] = List(6, 8)
Run Code Online (Sandbox Code Playgroud)
它看起来很痛苦......我甚至还没有开始尝试将它转换回元组.
我做错了吗?图书馆可以改进吗?