In all the exercises, we will look at variants of a File Scanner. The scanner finds and reports on the largest 10 files under a directory specified by the user, as well as collecting some stats about how many total files and total bytes are found.
This first exercise uses regular Scala features without Eff:
-
File system operations are done directly inline, primarily using the
java.nio.file.Files
API provided in Java 8. This is preferable to the operations offered through the olderjava.io.File
API because error conditions are signalled by an exception rather than simply returning an uninformativenull
. -
Error handling is by throwing exceptions which will bubble to the top.
Run the scanner on current directory with solutionExerciseClassic/run .
Does the result seem correct?
Try it on some larger directory of your computer.
Study the algorithm used by the scanner. The key data structure is a PathScan
, which contains a sorted list of the
largest N files and their sizes in bytes, plus a count of total files scanned and total bytes across all files.
Note how the scanner must combine PathScan
s of differing subdirectories together to yield
a single PathScan
that summarizes both the top N files and total files visited. This combine operation has a
Monoid structure.
Complete the Monoid instance for PathScan.
Run the unit tests with exerciseClassic/test
to verify your Monoid implementation.
Examine the ScannerSpec.
Note how much of the test is involved with creating- and cleaning up- actual files. It would be nice is we could separate testing the algorithm from the filesystem. How should this be done?