Quirky Clojure Functions

Malina Tran
Tech and the City
Published in
3 min readJul 19, 2016
From “Clojure for the Brave and True”

In my previous post, I highlighted some built-in functions in Clojure, which may be familiar to those who know a programming language. For the next few posts on Clojure, I’ll do a chapter summary of this book that I’m reading, Clojure for the Brave and True by Daniel Higginbotham.

Having read four chapters, I am more than qualified to attest to its coolness. Not only is the book accessible for beginners (see: not O’Reilly books), but it also clearly breaks down examples and comes equipped with humor and useful illustrations.

Clojure emphasizes programming to abstractions. What this means is that operations do not care about data type or implementation, but rather, whether they are able to perform on them. In chapter 4, sequence abstraction is differentiated from collection abstract; the former deals with operating on the individual elements of a sequence, and “seq” functions (as Clojurists will say) often convert their arguments to a seq and return a lazy seq. Lazy evaluation improves performance by delaying computations until they’re needed. Collection abstraction deals with data structures as a whole.

As I have already started my tic-tac-toe game in Clojure, I have familiarized myself with some functions. There are plenty that I have yet to implement, but strike me as incredibly useful. Here are some tidbits of knowledge that I’ve gathered:

  • Take-while can be more efficient than filter. `take-while` traverses a given sequence and applies the predicate function to each element. A predicate function is one whose return value is evaluated for truth or falsity to determine whether it should be taken or dropped. Similarly, `filter` returns all elements of a sequence that test true for a predicate function. The word of caution around use of `filter` is that it can end up processing all of your data, even though it is not necessary; meanwhile, `take-while` returns data without examining data that is not needed.
  • Lazy seqs allow you to construct infinite sequences. This is a pretty neat concept. One way of doing this is using `repeat` which creates a sequence based on arguments passed. Similarly, `repeatedly` calls on a function to generate each element in the sequence. There are functions, such as `first`, `cons` or `take`, that “realize” the lazy seq and returns to us an abridged sequence.
  • Convert the return value back with `into`. Many seq functions return a seq rather than its original data structure. By applying `into {}`, you can convert the return value back. Also, the first argument of `into` does not need to be empty.
  • Exploding a seqable data structure is possible. I’m not quite sure if this term was coined by Higginbotham himself, but the idea is that `apply` will “explode” the elements of a collection so that they get passed to a function as separate arguments. I really like how Clojure is able to do such things (like destructuring arguments based on their sequential order) and create flexibility around how we interact with data and functions.

“A partial takes a function f and fewer than the normal arguments to f, and returns a fn that takes a variable number of additional args. When called, the returned function calls with args + additional args.” -ClojureDocs

  • Partials are a little trippy. Admittedly, this took me awhile to grasp and even then, I’m not sure if I can adequately apply its use. Partials take any number of arguments (cool!) and returns a new function. When the returned function is called, it actually calls the original function with the original and new arguments. As a rule of thumb, partials should be used when there is repetition (aka non-DRY code) of the same combination of function and arguments in many different contexts.

Still getting to know Clojure a little bit better, one day at a time.

--

--