Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Scala集合中查找并替换项_Scala_Scala Collections - Fatal编程技术网

在Scala集合中查找并替换项

在Scala集合中查找并替换项,scala,scala-collections,Scala,Scala Collections,假设我有一个列表: val list = List(1, 2, 3, 4, 5) 我想替换满足谓词的所有/第一项,我知道以下方法:(例如,替换任何偶数为-1的数字) 简单高效: 更换所有项目 list.map( x => if (x % 2 == 0) -1 else x ) 替换一项 val index = list.indexWhere( _ % 2 == 0 ) list.updated(index, -1) 这是什么语言?:)您还应该删除,然后删除。但它很好,简单又有趣eff

假设我有一个列表:

val list = List(1, 2, 3, 4, 5)
我想替换满足谓词的所有/第一项,我知道以下方法:(例如,替换任何偶数为-1的数字)

简单高效: 更换所有项目

list.map( x => if (x % 2 == 0) -1 else x )
替换一项

val index = list.indexWhere( _ % 2 == 0 )
list.updated(index, -1)

这是什么语言?:)您还应该删除
,然后删除
。但它很好,简单又有趣effecient@JimN地图只有第一个问题
list.map( x => if (x % 2 == 0) -1 else x )
val index = list.indexWhere( _ % 2 == 0 )
list.updated(index, -1)