Below code demonstrates two methods of summing a value where the termination condition is if the value reaches 5 :
object ob extends App { def withoutRecursion = { var currentTotal = 0.0; while(5 > currentTotal){ currentTotal = currentTotal + scala.util.Random.nextFloat println(currentTotal); } } def withRecursion = { var currentTotal = 0.0; def whileLoop(cond : =>Boolean)(block : =>Unit) : Unit = if(cond) { block whileLoop(cond)(block) } whileLoop(5 > currentTotal) { currentTotal = currentTotal + scala.util.Random.nextFloat println(currentTotal); } } } Is the withRecursion method the idiomatic method of replacing a while loop with a functional programming style in this case ?