scala - 有条件地构建避免突变的列表
问题描述
假设我想有条件地建立比萨的成分列表:
val ingredients = scala.collection.mutable.ArrayBuffer("tomatoes", "cheese")
if (!isVegetarian()) {
ingredients += "Pepperoni"
}
if (shouldBeSpicy()) {
ingredients += "Jalapeno"
}
//etc
有没有使用不可变集合构建这个数组的功能性方法?
我想过:
val ingredients = List("tomatoes", "cheese") ++ List(
if (!isVegetarian()) Some("Pepperoni") else None,
if (shouldBeSpicy()) Some("Jalapeno") else None
).flatten
但有更好的方法吗?
解决方案
这是另一种更接近@Antot但恕我直言更简单的可能方式。
您的原始代码中不清楚的是来自何处isVegetarian
以及shouldBeSpicy
实际来自何处。在这里,我假设有一个PizzaConf
如下的类来提供这些配置设置
case class PizzaConf(isVegetarian: Boolean, shouldBeSpicy: Boolean)
假设这一点,我认为最简单的方法是使用一种allIngredients
类型List[(String, Function1[PizzaConf, Boolean])]
,即存储成分和功能以检查其相应可用性的类型。鉴于这buildIngredients
变得微不足道:
val allIngredients: List[(String, Function1[PizzaConf, Boolean])] = List(
("Pepperoni", conf => conf.isVegetarian),
("Jalapeno", conf => conf.shouldBeSpicy)
)
def buildIngredients(pizzaConf: PizzaConf): List[String] = {
allIngredients
.filter(_._2(pizzaConf))
.map(_._1)
}
或者您可以合并filter
和map
使用collect
如下:
def buildIngredients(pizzaConf: PizzaConf): List[String] =
allIngredients.collect({ case (ing, cond) if cond(pizzaConf) => ing })
推荐阅读
- amazon-web-services - AWS EBS 中的“卸载”和“分离”有什么区别?
- javascript - Node.JS、ExpressJS、React 和 HTML JSON 渲染
- r - 在 visNetwork 中的 Shiny 中以编程方式取消选择边缘
- git - 协调具有不同哈希的提交
- amazon-web-services - 通过 MYSQL 工作台连接到 RDS 实例,无需静态 IP
- java - 是否可以在 Java 中调用最终对象?
- vb.net - 来自游戏服务器的 UDP 统计响应
- c++ - 初始化 struct{enum;union;} 数组的问题
- c++ - 在 C++ 中从套接字接收短数组
- r - 按组对定性变量进行排序并保留索引