I'm trying to store the maximum price. There are some other business rules in there but the three nested if statements look very procedural and messy. I'm wondering if there is a nicer more functional way to express the following logic.
val sunsetTime1 = "14:00:00.000"
val maxPrices = new HashMap[String, Double]
if (trade.dateTime.before(time1)) {
if (maxPrices.contains(sunsetTime1)) {
if (maxPrices(sunsetTime1) < trade.price) {
maxPrices.put(sunsetTime1, price)
}
}
else {
maxPrices.put(sunsetTime1, price)
}
}
I'm not an Scala expert, and you should check that code, but I guess pattern matching and Options are more Idiomatic Scala.
if (trade.dateTime.before(time1)) {
maxPrices.get(sunsetTime1) match {
case Some(oldPrice) if oldPrice < trade.price => maxPrices.put(sunsetTime1, price)
case None => maxPrices.put(sunsetTime1, price)
}
}
Cheers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With