Composing a Solution

You can get a huge amount of programming mileage out of sequences and the panoply of functions that Clojure provides to process them. For example, imagine that you were working on a sales application for your book business and, starting with the books vector we saw earlier, you needed to format a string proclaiming the three top-rated books, something like this:

 "Emma // Dracula // Deep Six"

Step one is to find the top three books, by rating. We could start by sorting the books by their rating:

 (sort-by :rating books)

That will give us the books from lowest to highest rating, but the other way around is more useful:

 (reverse (sort-by :rating books))

Then we can then pull out the three highest-rated books, with take ...

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.