The following code has a lot of conditionals. I am trying to write it in a functional programming way.
val basePrice = {
var b = 0.0
if (runtime > 120)
b += 1.5
if ((day == Sat) || (day == Sun))
b += 1.5
if (!isParquet)
b += 2
if (is3D)
b += 3
b
}
I think the following code would be a good approach, but maybe I am complicating this too much.
val basePrice = {
List((runtime > 120, 1.5),
(day == Sat || day == Sun, 1.5),
(!isParquet, 2.0),
(is3D, 3.0)).foldLeft(0.0)((acum, cond) =>
if (cond._1) acum + cond._2 else acum)
}
How would you write the first snippet of code using functional programming?
Thanks