O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required