Laziness and Errors

Most Clojure data structures are lazy, which means that they are evaluated only as needed. To make these lazy structures play nicely with conditions (or even plain old exceptions, for that matter), you have to install your handlers around the code that actually realizes the collection, not around the code that creates the collection.

This can be confusing at the REPL. Can you spot the problem here?

 (with-handler
  (map parse-log-entry bad-log)
  (handle malformed-log-entry [msg]
  (continue-with nil)))
 -> java.lang.IllegalArgumentException: this is not a log message

This code is trying to add a handler, but it isn’t working. Stepping through the sequence of events will show why:

  1. The with-handler block sets a handler. ...

Get Functional Programming: A PragPub Anthology 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.