我有Java背景,最近开始学习Scala。我实现了一种对我来说仍然更像Java风格的方法。如何使用各种Scala功能将其改进为更像Scala的方法?
我有一个像
一样使用reduceLeft
的想法
val sales: List[Sale] = salesList.sortBy(_.timestamp)
sales.reduceLeft(someFunction)
该函数可能具有类似于
的签名 def reduceSalesFunction: (Sale, Sale) => Sale= {
???
}
这是我实现该方法的方式。有改进的空间吗?
def processSales(sales: List[Sale]): Sale = {
// if just a single sale, get it
if (sales.size == 1) sales.iterator.next
else {
// get the oldest sale
val sortedSales = sales.sortWith(_.timestamp < _.timestamp)
val oldestSale: Sale = sortedSales.min
val salesMetrics: Metrics = oldestSale.metrics
// update the oldest sale's metrics
for (sale <- sortedSales) {
val metrics: Metrics = sale.metrics
if (metrics.isMetric_1) salesMetrics.setIsMetric_1(metrics.isMetric_1)
if (metrics.isMetric_2) salesMetrics.setIsMetric_2(metrics.isMetric_2)
if (metrics.isMetric_3) salesMetrics.setIsMetric_3(metrics.isMetric_3)
}
// if there are metrics with `false` values set the oldest sale's metrics to `false` as well (Metrics is an Enum)
for (metric <- Metrics.values(); if !salesMetrics.isSet(metric)) {
salesMetrics.setMetricValue(metric, false)
}
oldestSale
}
}
答案 0 :(得分:1)
我为您提供一些样式建议:
getSomthting
,它不是吸气剂,我认为是
同样适用于Java。if else
替换为模式匹配。示例:
sales match {
// if just a single sale, get it
case x :: Nil => x
// get the oldest sale
case other => //your logic
}
选择以下一项:
val sortedSales: List[Sale] = sales.sortWith(_.timestamp < _.timestamp)
val oldestSale: Sale = sortedSales.min
val salesMetrics: Metrics = oldestSale.metrics
或
val sortedSales = sales.sortWith(_.timestamp < _.timestamp)
val oldestSale = sortedSales.min
val salesMetrics = oldestSale.metrics
isMetric_1
),请避免在命名方法/字段时使用“ _”。示例:
for {
metric <- Metrics.values()
if !salesMetrics.isSet(metric)
} salesMetrics.setMetricValue(metric, false)
PS:
这只是我的意见。这取决于您的团队!
我强烈建议您看看https://docs.scala-lang.org/style/。
享受Scala! :)