I have been studying scala for the past week or so and the ideologies associated with it and functional programming in general. As expected, the leap from imperative to functional programming is not as easy as I had hoped. For example, I converted a programming exercise that I did in Java into Scala. What I found was that it was near impossible for me to switch from mutable variables into the Scala style of immutable vals.
For the TL;DR people, my main question is, how does one decrease the var usage in a functional program? For the sake of a use case, I'll provide some sample code:
import scala.io._
object Bowling {
def main(args: Array[String]): Unit = {
val input = Source.fromFile("bowling.in").getLines
for(testCase <- input) {
val numberOfRows = augmentString(testCase).toInt
var index = 0
var a = 1
var sum = 0
while(index < numberOfRows) {
sum += a
a += 4
index += 1
}
println(sum)
}
}
}
EDIT: I'm accepting Karl's answer because of its conciseness though I do really appreciate Ptharien's code example and Doval's explanation of functional concepts as a whole.