A Change To The wc Application

Introduction

So the change is that your boss comes to you and says, “Um, err, yeah, I was wrong about that Word Count application. You won’t be getting a String that represents the document, you’ll be getting a String that is the name of the file you need to read, and it contains the document.”

This isn’t a huge change, but it does bring up some interesting concerns; what should you do if:

  • The file doesn’t exist?
  • The file isn’t readable?

In Java/OOP — and even Scala/OOP — you might throw exceptions and then let the callers of your code deal with those. But in Scala/EOP and Scala/FP we don’t do those things. We don’t throw exceptions because those would short-circuit our algebraic equations or blow up our blueprints.

Instead, we use a different technique known as functional error handling and error-handling data types. We’ll look at those next.