Prefer data over functions

Eric Normand over at LispCast has a great post detailing his thoughts on why data should be preferred over functions, which in turn should be preferred over macros. If you’ve done functional programming in a Lisp before, you may have run into the design issue of whether to model your problem in terms of functions or data. For instance, let’s say you want to calculate the average of a set of numbers. You could easily do it in Clojure with functions:

(def numbers [1 2 3 4])
(/ (apply + numbers) (count numbers)) ; => 5/2

Alternatively, you could model it by creating a mini-DSL – custom data structures which are passed to an interpreter function to produce a result:

(def expression [:div [:add numbers] [:count numbers]])
(calculate expression) ; => 5/2

In this case, using functions gets the job done much faster, and is easier to understand. However, Eric argues why you might prefer using data:

  1. Structure of the data is transparent at runtime, allowing it to be inspected and analysed
  2. Data can be manipulated easily – most Lisps have a rich set of functionality for dealing with data structures
  3. Data can be interpreted in different ways
  4. DSLs allow a simpler semantic model to be imposed – this is usually the reason for writing a DSL in the first place and makes it easier to write custom tools such as editors

Eric pragmatically ends the discussion with his own personal guideline that code should be written as functions first, and then refactored to data only when the problem domain has been well understood. This helps to prevent cases where the DSL has to be extended in all kinds of clunky ways, because the problem domain expanded or was not covered adequately.

Link

core.async for Asynchronous Programming

If you’re looking for a new language to try, Clojure is a particularly powerful option to pick. It’s a modern Lisp that can run on the JVM, JavaScript and .NET. This means that you can leverage the power and reach of an existing mature ecosystem – from full-stack web applications to desktop applications with Swing or NodeJS, and even writing games in Unity3D or Android.

To whet your appetite, here are a couple of videos on core.async, a Clojure/ClojureScript library for asynchronous programming that implements Communicating Sequential Processes (CSP) – a concept popularised by the Go language. Using channels and message-passing, CSP offers an elegant way to deal with concurrency and event handling without falling into callback hell.

It’s an indication of the power of Lisp macros that CSP can be implemented as a library without special support from the compiler, on top of both the JVM and JavaScript (which inherently does not support the multi-threaded style of programming).

If you’d like to check out Clojure further, I recommend this interactive course from the University of Helsinki. Clojure for the Brave and True by Daniel Higginbotham is another great place to start.

core.async for Asynchronous Programming