我有几种方法对(for (i <- 0 until y.length) yield y(i) + 0.5*dy1(i)) toVector
序列进行操作,当使用for comprehension / yield将来自多个向量的数据组合成一个时,下面的习惯用法很常见:
toVector
注意结束type mismatch;
found : scala.collection.immutable.IndexedSeq[Double]
required: Vector[Double]
和围绕for comprehension的括号。我想摆脱它,因为它很难看,但删除它会产生以下错误:
toVector
有没有更好的方法来实现我想要的,避免显式调用 function editNotifications() {
var popUp = $ionicPopup.show({
template: '<ion-checkbox ng-repeat="item in vm.notifications" ng-model="item.notification" ng-checked="item.notification">' +
'<span class="notificationsFont">{{item.settingsText}}</span></ion-checkbox>',
title: 'Notification Settings',
scope: $scope,
buttons: [
{
text: 'Cancel',
onTap: function(e) {
vm.notifications = localStorageManager.get('notificationStatus');
}
},
{
text: '<b>Ok</b>',
type: 'button-positive',
onTap: function(e) {
localStorageManager.set('notificationStatus', vm.notifications);
vm.notificationText = "Notifications off";
setNotificationValuesBasedOnUserInput(vm.notifications);
}
}]
});
};
多次基本上实现非操作(转换和索引序列...到索引序列)?
答案 0 :(得分:1)
避免集合投射的一种方法,例如toVector
,如果可能,仅调用那些返回相同集合类型的方法。
y.zipWithIndex.map{case (yv,idx) => yv + 0.5*dy1(idx)}
答案 1 :(得分:0)
for yield
Range
默认情况下会产生Vector[T]
。
例如,
scala> val squares= for (x <- Range(1, 3)) yield x * x
squares: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 4)
检查类型,
scala> squares.isInstanceOf[Vector[Int]]
res14: Boolean = true
请注意,Vector[T]
也会延伸IndexedSeq[T]
。
@SerialVersionUID(-1334388273712300479L)
final class Vector[+A] private[immutable] (private[collection] val startIndex: Int, private[collection] val endIndex: Int, focus: Int)
extends AbstractSeq[A]
with IndexedSeq[A]
with GenericTraversableTemplate[A, Vector]
with IndexedSeqLike[A, Vector[A]]
with VectorPointer[A @uncheckedVariance]
with Serializable
with CustomParallelizable[A, ParVector[A]]
这就是为什么上述结果也是IndexedSeq[T]
,
scala> squares.isInstanceOf[IndexedSeq[Int]]
res15: Boolean = true
您可以将结果类型定义为IndexedSeq[T]
,并且在未明确调用Vector
.toVector
达到您想要的效果
scala> val squares: IndexedSeq[Int] = for (x <- Range(1, 3)) yield x * x
squares: IndexedSeq[Int] = Vector(1, 4)
scala> squares == Vector(1, 4)
res16: Boolean = true
但是Seq[T]
的收益率默认为List[T]
。
scala> val squares = for (x <- Seq(1, 3)) yield x * x
squares: Seq[Int] = List(1, 9)
只有在这种情况下,如果你想要矢量,你必须.toVector
结果。
scala> squares.isInstanceOf[Vector[Int]]
res21: Boolean = false
scala> val squares = (for (x <- Seq(1, 3)) yield x * x).toVector
squares: Vector[Int] = Vector(1, 9)