Factorial method with accumulator method

So I have found myself a neat recursive method to write factorials with the accumulator. Accumulator is often used, as many may know in Spark applications, and very useful. It looks like this:

def factorial(i: Int): Int = {
def fact(i: Int, accumulator: Int): Int = {
if (i <= 1)
accumulator
else
fact(i - 1, i * accumulator)
}
fact(i, 1)
}
So now, if I want to let it print 10 times, I actually have to type
println( factorial(x)) )
10 times…
I tried mutliple ways, like println( factorial(1 … 10) )
but to no avail.
Can someone tell me how can I write this down as a one-liner?

How about “(1 to 10).map(factorial).foreach(println)”? Of course that has some redundancy in calculation – if you are looking to avoid the redundancy, then you’d want to alter how your factorial function is defined.

Brian Maso

or you could use foreach.

Here is an interesting and educational approach to your problem of printing the list of factorials.
Assumption:
What if the factorial function was a heavy time consuming computation. And you needed to generate a list of solutions generated by calling that function. One popular solution is the use some type of internal caching device so that you would not have to recalculate factorials for numbers that had already been calculated. That approach can be left as an exercise for anyone that wishes to investigate it.
However, if caching was not an option, here is an interesting asynchronous approach to generating a list of results from some computationally intensive function:

Assuming that you run this in a simple Scala main function, you need a way of preventing the main function from exiting since everything is asynchronous in this implementation:
/** latch is just for convenience so you can see the result in the main application **/
val latch = new CountDownLatch(1)

/** now we just execute each factorial call in a Future **/
val futs = Future.sequence( for ( i <- ( 1 to 10 )) yield {
Future { factorial(i) }}).foreach { p => println§; latch.countDown() }

Explanation:
The for comprehension will list a list of Future Objects that may or may not have completed.
The Future.sequence function will convert the List of Futures to a Future of Lists.
The final foreach is applied against a Future of List and will complete when all computations complete.
Not the latch.countDown() is for convenience only and would not be needed in a true server application. Not shown here, but the last statement would be latch.await().

I am currently studying scala in my free time and am currently going over “Futures” which I find infinitely interesting.

(2 to 10).scanLeft(1)(_*_)

I would argue that is the most efficient implementation in Scala for getting a list of factorials. It is pretty minimal on keystrokes as well. Granted, it is less obvious what it is doing. The reader has to understand scanLeft, which means they probably need to be familiar with foldLeft.

It is worth noting that because factorial quickly outgrows the bounds of an Int, this expression can be made to work with a BigInt with minor modification.

(2 to 100).scanLeft(BigInt(1))(_*_)