Calculating a base price with surcharge conditions 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 &gt; 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 &gt; 120, 1.5), 
       (day == Sat || day == Sun, 1.5),
       (!isParquet, 2.0),
       (is3D, 3.0)).foldLeft(0.0)((acum, cond) =&gt;
     if (cond._1) acum + cond._2 else acum)
}


How would you write the first snippet of code using functional
programming?
Each condition is a function. It might be that you could write it more concisely, but I think the code is clearer if you do this:

def priceFunction(cond: =&gt; Boolean)(mod: Double =&gt; Double) = (_: Double) match {
    case x if cond =&gt; mod(x)
    case y =&gt; y
}

val modRuntime = priceFunction(runtime &gt; 120)(_ + 1.5)
val modWeekend = priceFunction(day == Sat || day == Sun)(_ + 1.5)
val modParquet = priceFunction(!isParquet)(_ + 2.0)
val mod3d = priceFunction(is3D)(_ + 3.0)
val modifiers = List(
    modRuntime,
    modWeekend,
    modParquet,
    mod3d
)
val modifierFunction = modifiers reduceLeft (_ andThen _)                   

val basePrice = modifierFunction(0.0)


The name of the identifiers here suck, and I could have written val modifiers = modRuntime andThen modWeekend andThen modParquet andThen mod3d without trouble. I choose putting them in a List because it shows how well it can scale.

One could also make PartialFunction and chain them with orElse, for the cases where you want only the first condition.

You see this kind of thing used in web frameworks, such as BlueEyes, Lift or Unfiltered, for example.