concept loop in category scala

This is an excerpt from Manning's book Get Programming with Scala.
Have a look at figure 5.2 for a syntax diagram of while loops in Scala: in a while loop, you repeat its instructions until its condition is no longer valid.
Figure 5.2: Syntax diagram of while loops in Scala: you evaluate the expression until the condition is valid.
![]()
While loops are part of the Scala language, but they are rarely used and often considered to be an anti-pattern.
While loops operate on mutable state: they keep track of how it evolves, and they stop once a predicate is respected. Scala discourages the use of mutability, so while loops are quite rare in the code. In unit 4, you’ll learn about how the Standard Scala Collections provides powerful alternatives to while loops through dedicated functions, such as foreach and map, to iterate in a functional and thread-safe way.
5.4: Repeating using a for loop
def echo5(msg: String) = { for (i <- 0 to 5) { //#A println(msg) } }When using a for loop, you are repeating an operation over a finite set of values, rather than using a stop condition. Have a look at figure 5.3 for a summary of for loops in Scala.
Figure 5.3: Syntax diagram on for loops in Scala. In a for loop, an expression is repeated for each of a finite set of values.
![]()
Scala also offers a more powerful alternative to the classic for loop you have just seen, called for-comprehension: you are going to learn about it when discussing the class Option in unit 4.

This is an excerpt from Manning's book Functional Programming in Scala.
We’ll provide a more precise definition of side effects later in this chapter, but consider what programming would be like without the ability to do these things, or with significant restrictions on when and how these actions can occur. It may be difficult to imagine. How is it even possible to write useful programs at all? If we can’t reassign variables, how do we write simple programs like loops? What about working with data that changes, or handling errors without throwing exceptions? How can we write programs that must perform I/O, like drawing to the screen or reading from a file?
Once we’ve covered some of the basic elements of the Scala language, we’ll then introduce some of the basic techniques for how to write functional programs. We’ll discuss how to write loops using tail recursive functions, and we’ll introduce higher-order functions (HOFs). HOFs are functions that take other functions as arguments and may themselves return functions as their output. We’ll also look at some examples of polymorphic HOFs where we use types to guide us toward an implementation.
Listing 2.3. Monomorphic function to find a String in an array
def findFirst(ss: Array[String], key: String): Int = { @annotation.tailrec def loop(n: Int): Int = if (n >= ss.length) -1 #1 else if (ss(n) == key) n #2 else loop(n + 1) #3 loop(0) #4 } #1: If n is past the end of the array, return -1, indicating the key doesn’t exist in the array. #2: ss(n) extracts the nth element of the array ss. If the element at n is equal to the key, return n, indicating that the element appears in the array at that index. #3: Otherwise, increment n and keep looking. #4: Start the loop at the first element of the array.
The way we write loops functionally, without mutating a loop variable, is with a recursive function. Here we’re defining a recursive helper function inside the body of the factorial function. Such a helper function is often called go or loop by convention. In Scala, we can define functions inside any block, including within another function definition. Since it’s local, the go function can only be referred to from within the body of the factorial function, just like a local variable would. The definition of factorial finally just consists of a call to go with the initial conditions for the loop.
The arguments to go are the state for the loop. In this case, they’re the remaining value n, and the current accumulated factorial acc. To advance to the next iteration, we simply call go recursively with the new loop state (here, go(n-1, n*acc)), and to exit from the loop, we return a value without a recursive call (here, we return acc in the case that n <= 0). Scala detects this sort of self-recursion and compiles it to the same sort of bytecode as would be emitted for a while loop,[3] so long as the recursive call is in tail position. See the sidebar for the technical details on this, but the basic idea is that this optimization[4] (called tail call elimination) is applied when there’s no additional work left to do after the recursive call returns.