Data definition in Clojure
07 Dec 2016Introduction
In today’s article, I’ll briefly go over representing record-like information in Clojure.
Maps
First off, we’ll take a quick tour using a Map.
A
Map
is a collection that maps keys to values
So this is a pretty fundamental data type that we can use. We can pretty easily represent a person using it:
We can use count to count the number of pairs in the map.
We can use conj to give our person a hair
attribute.
To navigate the map a little bit we can use get, contains?, find, keys, vals.
Using seq you can return a seq over the pairs; on each item in the seq you can use key to get the value of the key and val to get the value.
This gives us a full key/value pairing structure for us to arbitrarily represent data in a organized fashion; but not organized enough.
Records
Using records or defrecord we can turn a list of keys into a type that we can repeatably construct.
We’re afforded all of the same functions above to work on this value; but we’re given the positional factory function ->Constructor so that we can construct our types in a much more intuitive way.
Domain functions
Now that we’ve spent a little bit of time creating data structures and defining data records, we can create functions that will allow us to perform operations on this data. Domain functions in Clojure perform operations specific to the data structure. In this following example, we’ve created a function to format a person’s full name:
Object-oriented programming gives us polymorphism by allowing us to implement a class’s method differently per type that we derive from a common base. Clojure, gives us this multi-dispatch effect (choosing the right function for the given data-type) through multi methods and protocols.
Multi-methods
We’re going to use the defmulti macro to define our multi method and we’ll provide implementations using the defmethod macro. From the documentation:
Clojure eschews the traditional object-oriented approach of creating a new data type for each new situation, instead preferring to build a large library of functions on a small set of types. However, Clojure fully recognizes the value of runtime polymorphism in enabling flexible and extensible system architecture. Clojure supports sophisticated runtime polymorphism through a multimethod system that supports dispatching on types, values, attributes and metadata of, and relationships between, one or more arguments.
We’re going to expand the Person
example above, by adding a new record of Company
.
Then we’ll define our multi method called get-full-name
. Its job is to put together the name of our entity. Because we have both a Person
and Company
type entity, we’re going to need two different implementations:
The Person
implementation of the get-full-name
function concatenates the :first-name
and :last-name
attributes together, where as the Company
implementation need only return the :name
attribute.
Something that is interesting and unique to multi-methods is value-based dispatch; we’ve already seen type-based dispatch.
Consider a temperature conversion between Fahrenheit and Celsius. We create our multi method the same way, but this time we need to give the parameter values identity:
We can now give our multi method some implementations based on the source and destination units passed:
We can now test out that the dispatching works:
Protocols
Protocols are a little more reminiscent of object-oriented programming in a sense that they are closely related to interfaces. From the documentation:
Clojure is written in terms of abstractions. There are abstractions for sequences, collections, callability, etc. In addition, Clojure supplies many implementations of these abstractions. The abstractions are specified by host interfaces, and the implementations by host classes. While this was sufficient for bootstrapping the language, it left Clojure without similar abstraction and low-level implementation facilities. The protocols and datatypes features add powerful and flexible mechanisms for abstraction and data structure definition with no compromises vs the facilities of the host platform.
So, protocols give us a way to defining abstractions. We can treat our Person
and Company
scenario as such, by calling them a Party
.
We use defprotocol to start our abstraction definition. extend-protocol is then used to supply implementations.
Wrapping up
This has been a brief tour on creating map/record data and some domain functions to work with them.