Tail call optimization
We are familiar with the limitations recursion brings with it. We are aware that each function call, if not tail recursive, builds a new stack frame. For scenarios where we have to deal with a large number of function calls, this could possibly result in a stack overflow, which is undesirable. So, what's suggested in this kind of scenario is to make the recursive function call the last statement in your function definition, the Scala compiler then does the rest for you. Take a look at the following:
import scala.annotation.tailrec object TailRecursion { def main(args: Array[String]): Unit = { val list = List("Alex", "Bob", "Chris", "David", "Raven", "Stuart") someRecursiveMethod(list) } /* You have a sorted list of names of employees, within a company. print all names until the name "Raven" comes */ @tailrec def someRecursiveMethod(list: List[String]): Unit = { list match { case Nil => println("Can...