Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/16.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_Collections_Functional Programming_Scala Collections - Fatal编程技术网

Scala 如果默认项为空,是否有较短的方法用默认项填充集合

Scala 如果默认项为空,是否有较短的方法用默认项填充集合,scala,collections,functional-programming,scala-collections,Scala,Collections,Functional Programming,Scala Collections,假设有一个cars列表 我想添加法拉利项目,前提是集合为空,并返回最终集合 我这样做: val finalCarsSet = Option(cars) filterNot(_.isEmpty) getOrElse Set("Ferrari") 是否有一种使用高阶函数的较短方法?为什么要将列表包装在选项中?您可以使用isEmpty: scala> Set() res0: scala.collection.immutable.Set[Nothing] = Set() scala>

假设有一个
cars
列表

我想添加
法拉利
项目,前提是集合,并返回最终集合

我这样做:

val finalCarsSet = Option(cars) filterNot(_.isEmpty) getOrElse Set("Ferrari") 

是否有一种使用高阶函数的较短方法?

为什么要将列表包装在选项中?您可以使用
isEmpty

scala> Set()
res0: scala.collection.immutable.Set[Nothing] = Set()

scala> if(res0.isEmpty) Set("Ferrari") else res0
res1: scala.collection.immutable.Set[_ <: String] = Set(Ferrari)

scala> Set("Ferrari", "Porsche")
res2: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)

scala> if(res2.isEmpty) Set("Ferrari") else res2
res3: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)
scala>Set()
res0:scala.collection.immutable.Set[Nothing]=Set()
scala>if(res0.isEmpty)Set(“法拉利”)else res0
res1:scala.collection.immutable.Set[uu)Set(“法拉利”、“保时捷”)
res2:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)
scala>if(res2.isEmpty)Set(“法拉利”)else res2
res3:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)

从代码中我不明白的一件事是,如果您只想返回带有“Ferrari”的套件,或者您想将汽车添加到您已有的套件中。

为什么要将列表包装在选项中?您可以使用
isEmpty

scala> Set()
res0: scala.collection.immutable.Set[Nothing] = Set()

scala> if(res0.isEmpty) Set("Ferrari") else res0
res1: scala.collection.immutable.Set[_ <: String] = Set(Ferrari)

scala> Set("Ferrari", "Porsche")
res2: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)

scala> if(res2.isEmpty) Set("Ferrari") else res2
res3: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)
scala>Set()
res0:scala.collection.immutable.Set[Nothing]=Set()
scala>if(res0.isEmpty)Set(“法拉利”)else res0
res1:scala.collection.immutable.Set[uu)Set(“法拉利”、“保时捷”)
res2:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)
scala>if(res2.isEmpty)Set(“法拉利”)else res2
res3:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)

从代码中我不明白的一件事是,如果您只想返回带有“Ferrari”的套件,或者您想将汽车添加到您已有的套件中。

为什么要将列表包装在选项中?您可以使用
isEmpty

scala> Set()
res0: scala.collection.immutable.Set[Nothing] = Set()

scala> if(res0.isEmpty) Set("Ferrari") else res0
res1: scala.collection.immutable.Set[_ <: String] = Set(Ferrari)

scala> Set("Ferrari", "Porsche")
res2: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)

scala> if(res2.isEmpty) Set("Ferrari") else res2
res3: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)
scala>Set()
res0:scala.collection.immutable.Set[Nothing]=Set()
scala>if(res0.isEmpty)Set(“法拉利”)else res0
res1:scala.collection.immutable.Set[uu)Set(“法拉利”、“保时捷”)
res2:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)
scala>if(res2.isEmpty)Set(“法拉利”)else res2
res3:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)

从代码中我不明白的一件事是,如果您只想返回带有“Ferrari”的套件,或者您想将汽车添加到您已有的套件中。

为什么要将列表包装在选项中?您可以使用
isEmpty

scala> Set()
res0: scala.collection.immutable.Set[Nothing] = Set()

scala> if(res0.isEmpty) Set("Ferrari") else res0
res1: scala.collection.immutable.Set[_ <: String] = Set(Ferrari)

scala> Set("Ferrari", "Porsche")
res2: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)

scala> if(res2.isEmpty) Set("Ferrari") else res2
res3: scala.collection.immutable.Set[String] = Set(Ferrari, Porsche)
scala>Set()
res0:scala.collection.immutable.Set[Nothing]=Set()
scala>if(res0.isEmpty)Set(“法拉利”)else res0
res1:scala.collection.immutable.Set[uu)Set(“法拉利”、“保时捷”)
res2:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)
scala>if(res2.isEmpty)Set(“法拉利”)else res2
res3:scala.collection.immutable.Set[String]=Set(法拉利、保时捷)


有一件事我不明白你的代码是,如果你想返回一套只有“法拉利”或者你想把车添加到你已经拥有的集合中。

但是我试图避免使用条件。当然,使用条件我可以。如果没有比条件更短的方法,我会使用它;)因为你的最后一句话=>
集合
在我的情况下是不可变的。老实说,条件化可以使你的代码更短,我会说得更清楚我同意你的观点,但我想知道是否有一种神奇的方法可以让我忽略;)这不是我能想到的,可能有人有更好的选择:)我真的认为你是对的。我只能在5分钟内验证你的答案(堆栈溢出约束):)我会的。谢谢。但是我试着避免使用条件句。当然,使用条件句我可以。如果没有比条件句更短的方法,我会使用它;)因为你的最后一句话=>
Set
在我的情况下是不可变的。老实说,在这里,条件句可以使你的代码更短,而且我认为比将列表包装成另一种类型更易读我同意你的观点,但我想知道是否有一种神奇的方法可以让我忽略;)这不是我能想到的,可能有人有更好的选择:)我真的认为你是对的。我只能在5分钟内验证你的答案(stackoverflow约束):)我会的。谢谢。但是我试着避免使用条件句。当然,使用条件句我可以。如果没有比条件句更短的方法,我会使用它;)对于你的最后一句话=>
Set
在我的情况下是不可变的。老实说,在这里,条件句可以使你的代码更短,而且我认为比把列表包装成另一个句子更清晰我同意你的观点,但我想知道是否有一种神奇的方法可以让我忽略;)这不是我能想到的,可能有人有更好的选择:)我真的认为你是对的。我只能在5分钟内验证你的答案(stackoverflow约束):)我会的。谢谢。但是我试着避免使用条件句。当然,使用条件句我可以。如果没有比条件句更短的方法,我会使用它;)对于你的最后一句话=>
Set
在我的情况下是不可变的。老实说,在这里,条件句可以使你的代码更短,而且我认为比把列表包装成另一个句子更清晰我同意你的观点,但我想知道是否有一种神奇的方法可以让我忽略;)这不是我能想到的,可能有人有更好的选择:)我真的认为你是对的。我只能在5分钟内验证你的答案(stackoverflow约束))我会的,谢谢