0 votes
in Scala Constructs by
How do we implement loops functionally in Scala? What is the difference between OOP and FP style loops?

1 Answer

0 votes
by

We know how to implement loops in Object-Oriented style: Using Mutable Temporary variables, update the variable value and use Loop constructs. It is very tedious and unsafe approach. It is not Thread-Safe. Object-Oriented style uses the following constructs to implement Loops:

  1. Loop Constructs
  2. Mutability
  3. Side Effects

We can implement same Loops differently in Functional way. It is Thread-Safe. We can use the following two techniques to implement the loops in functional style:

  1. Recursion
  2. Tail-Recursion
  3. Immutability
  4. No Side-Effects
...