Posted on by & filed under Content - Highlights and Reviews, Programming & Development.

Edmund Jackson (@edmundjackson) is an independent consultant in all things Clojure and Data Science, and he is a member of LambdaNext, a Clojure training and consulting group.

Having the core constructs of Clojure available as values allows some simplifying tricks that might not be obvious when coming from a mutable language. This post will cover some useful examples.

Keys in Hashmaps

The key in a hashmap must be hashable (funny, that), and any mutable thing is not. So in most languages you are severely restricted by what you can use as keys in your hashmaps. Usually this comes down to numbers, strings, and keywords, which are simple types. Frequently though, you may want to do something more complex, like use a compound value such as a container or vector as the key. So, you must serialize the container to get a string and use that. As a result, you have a whole heap of complexity going in and out of the hashmap. In Clojure, as the vector is a value and is hashable, it can be used directly as the key in a hashmap:

This is a valid hashmap. This is a trick that can be used to avoid deeply nested maps by representing the layers of nesting in the vector, which for some access patterns is simplifying. It’s not just vectors: maps, sets, and functions – anything immutable – can be used as a key in a hash-map:


Another great trick made available by values is ubiquitous memoisation. Again, you can memoize anything that is immutable. In mutable-everywhere languages, you run into the same problem with collections described for hashmaps, and for this same reason it isn’t a problem in Clojure. Combined with the good practice of small, composable functions, memoisation becomes very powerful. You can pull out the time-consuming elements of an algorithm or process and, by memoising, trade memory for time. Depending upon what you’re doing, this can dramatically speed up processes doing repeated calculations or IO operations such as DB access or remote API calling. The point is not that this is impossible elsewhere, but that it is easy to implement in Clojure. This allows you to write simple code, directly capturing the algorithm/process, and use memoisation to cleanly optimize it.

A frequently encountered problem with memoisation is that it is, by definition, a memory hole, which can limit its usefulness. This can be flexibly addressed with core.memoize, which allows you to plug in a caching strategy of your choice to back the memoisation. By choosing this to match the data processing pipeline, you can sometimes have your cake and eat it too.

Safari Books Online has the content you need

Check out these Clojure books available from Safari Books Online:

Clojure Inside Out is a video where you’ll not only learn how to tackle practical problems with this functional language, but you’ll learn how to think in Clojure—and why you should want to. Neal Ford (software architect and meme wrangler at ThoughWorks) and Stuart Halloway (CEO of Relevance, Inc.) show you what makes programming with Clojure swift, surgical, and accurate.
Clojure Programming, helps you learn the fundamentals of Clojure with examples relating it to the languages you know already—whether you’re focused on data modeling, concurrency and parallelism, web programming, statistics and data analysis, and more.
Practical Clojure is the first definitive reference for the Clojure language, providing both an introduction to functional programming in general and a more specific introduction to Clojure’s features. This book demonstrates the use of the language through examples, including features such as STM and immutability, which may be new to programmers coming from other languages.
The Joy of Clojure goes beyond the syntax, and shows how to write fluent, idiomatic Clojure code. You will learn to approach programming challenges from a Functional perspective and master the Lisp techniques that make Clojure so elegant and efficient. This book will help you think about problems the “Clojure way,” and recognize when they simply need to change the way they program.

About the author

edmund Edmund Jackson (@edmundjackson) is an independent consultant in all things Clojure and Data Science. Edmund is a member of LambdaNext, a Clojure training and consulting group. Currently based in Cambridge, UK he is looking forward to moving to the US at the end of 2013.

Tags: Clojure, Hashmaps, Memoisation, Tricks, Values,

Comments are closed.