Programming to Abstractions
Clojure’s spit and slurp I/O functions are built on two abstractions, reading and writing. This means you can use them with a variety of source and destination types, including files, URLs, and sockets, and they can be extended to support new types by anybody, whether they’re existing types or newly defined.
-
The slurp function takes an input source, reads the contents, and returns it as a string.
-
The spit function takes an output destination and a value, converts the value to a string, and writes it to the output destination.
We’ll start by writing basic versions of the two functions that can read from and write to files only. We’ll then refactor the basic versions several times as we explore different approaches ...