One Thing After Another

Let’s start our exploration of sequences by asking a simple question: how does the count function work? Specifically, how does this one function manage to count the elements in a vector, the items in a list, the characters in a string, and the entries in a map? After all, we know that behind the scenes lists and vectors and maps and strings are all very different data structures, and if you want to count the elements of each one, somewhere there needs to be code to deal with the structural differences.

One possible design for this would be to write in special-case code for each collection type, maybe by using a multimethod. So if we were building our own count, we might write this:

 ;; Is this how ...

Get Getting Clojure now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.