val numbers = List(1, 2, 3, 4, 5, 6, 7, 8)

//Imperative style
var total1 = 0
for(i <- numbers) {
  total1 += i
}

println("Total is " + total1)

//Functional style - no mutablity, no side effect in the code, uses higher order function foldLeft
val total2 = numbers.foldLeft(0) { (carry, element) => carry + element }
println("Total is " + total2)

//Many simplifications of the above can be realized, depending on your comfort level with Scala as you get familiar
//a+b is really a.+(b). Colon change the order of operands, so a op_: b is the same as b.op_:(a);
//foldLeft comes in another flavor, the /: method.

val total3 = (0 /: numbers) { (carry, element) => carry + element }
println("Total is " + total3)

//_ represents the parameter, each occurance represents one parameter, so we can use
val total4 = (0 /: numbers) { _ + _ }
println("Total is " + total4)

//Scala can also implicity pass parameters. Say you want to find the max value among the numbers,
//you can do either of the following:

println("Max is " +  (Integer.MIN_VALUE /: numbers) { (carry, element) => math.max(carry, element) })

println("Max is " +  (Integer.MIN_VALUE /: numbers) { math.max })
