Clojure Map Keys as Functions

A twist on a previous post.

August 27, 2021

This is a follow-up to a previous post which described a convenient feature in clojure of maps also serving as lookup functions. Well, it goes the other way: map keys can also be used as lookup functions! Consider the following data:

(def users
  [{:name "Peter"}
   {:name "James"}
   {:name "John"}])

Suppose you wanted just the name values in a sequence. Here's one way:

user=> (map #(% :name) users)
("Peter" "James" "John")

But, since the map key itself can serve as a function, why not do it this way?

user=> (map :name users)
("Peter" "James" "John")

So elegant!