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 are 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 will start by writing basic versions of the two functions that can read from and write to files only. We will then refactor the basic versions several times as we explore different approaches ...

Get Programming Clojure, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.