最简洁的方式来组合序列元素 [英] Most concise way to combine sequence elements
问题描述
假设我们有两个序列,我们和我们想用一些方法来组合它们。 2,3)
val b =向量(4,5,6)
例如另外可以是
val c = a zip b map {i => i._1 + i._2}
或
val c = a zip b map {case(i,j)=> i + j}
第二部分的重复使我认为这应该可以在单个操作中实现。我看不到任何内置方法。我想我真正想要的是一种zip方法,它可以跳过元组的创建和提取。
在普通的Scala中有没有更漂亮/更简洁的方式,或者可能与Scalaz ?如果没有,你会如何编写这样的方法,并将其压缩到序列中,以便我可以编写类似于
val c =一个zipmap b(_ + _)
>
(a,b).zipped.map(_ + _)
这可能已经足够接近你想要的扩展名了。 (不幸的是,你不能使用它,因为压缩
中的含义不喜欢这样。)
Say we have two sequences and we and we want to combine them using some method
val a = Vector(1,2,3)
val b = Vector(4,5,6)
for example addition could be
val c = a zip b map { i => i._1 + i._2 }
or
val c = a zip b map { case (i, j) => i + j }
The repetition in the second part makes me think this should be possible in a single operation. I can't see any built-in method for this. I suppose what I really want is a zip method that skips the creation and extraction of tuples.
Is there a prettier / more concise way in plain Scala, or maybe with Scalaz? If not, how would you write such a method and pimp it onto sequences so I could write something like
val c = a zipmap b (_+_)
There is
(a,b).zipped.map(_ + _)
which is probably close enough to what you want to not bother with an extension. (You can't use it point-free, unfortunately, since the implicits on zipped
don't like that.)
这篇关于最简洁的方式来组合序列元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!