Get startedGet started for free

foreach and the functional style

1. foreach and the functional style

In this lesson, you'll see how the functional style differs from the imperative style.

2. Scala is functional

Previously, you saw this slide.

3. Scala is a imperative/functional hybrid

Scala is ACTUALLY a hybrid imperative/functional language, where the functional style is preferred.

4. Scala nudges us towards the functional style

Scala doesn't FORCE us to be functional, but it does nudge us.

5. "imperative" according to Oxford Dictionary

The dictionary definition of imperative is "of the nature of or expressing a command."

6. The imperative style

Imperative PROGRAMMING means one command at a time, iterating with loops, mutating shared state. You can program imperatively in Scala if you want to.

7. The imperative style

Here's the while loop from a previous lesson. Feature 1 of imperative programming: one command at a time. Look inside the while loop. One command: print if a hand busts. Another command: increment the counter variable. Feature 2: iterate with loops. The while loop.

8. The imperative style

Feature 3: mutating shared state. var i can be changed within the loop (which is one scope) or outside the loop (another scope). It is mutated in the loop in this example.

9. The functional style

Since Scala was designed functional-first, the functional style is favored because it comes with the biggest benefits in Scala, which you'll learn about next lesson. One aspect of functions being first-class values is that functions can be passed as arguments to functions. Leveraging this, we convert the while loop

10. The functional style

from this

11. The functional style

to this using the foreach method. foreach is not a built-in control structure; it is a method that takes a function as an argument, which is a demonstration of Scala the functional language.

12. Scala fuses OOP and FP -> Scala is scalable

Side note: that the hands array is an object and has a method called foreach demonstrates Scala the object-oriented language. Both keys to scalability from the first chapter are in this code.

13. Modify the bust function

Let's modify the bust function for now so its function body performs the printing of whether or not a hand busts.

14. The functional style

We insert the new bust

15. The functional style

function and get the same output as the while loop: false, true, false. Cleaner, more concise code -- five lines vs. eleven.

16. What is a side effect?

The second part of Scala being functional means side effects should be avoided, where a side effect means code modifying some variable outside of its local scope.

17. What is a side effect?

Though it sounds a little weird, printing to the standard output stream is a side effect. When we provide 22 as an input to bust then assign the result to a val named myHand in the interpreter, we see two lines of output: true, which is printed by the print line call, and the output from declaring the val myHand, which has type Unit. The input value (hand) no longer maps to a true output value with the print statement present. More on Unit soon.

18. What is a side effect?

Mutating a variable outside of its scope is also a side effect. Since i was declared OUTSIDE of the while loop, which is one scope, incrementing i in the loop (another scope) is a side effect.

19. The spectrum of functional style

That said, there is a spectrum for functional-style code. When we added println to the bust function, our code became less functional in style. When we converted while to foreach, our code became MORE functional. As a whole, I'd argue that our code become MORE functional, but not purely functional. That's because foreach is designed for side effects, which you can see in the documentation.

20. Signs of style

vars, side effects, and Unit are telltale signs of imperative-style code. If you notice vals, no side effects, and non-Unit value types like Int, Boolean, Double, you're probably looking at functional-style code. More on this in more advanced courses.

21. Let's practice!

To be functional, one needs to practice.