Search code examples
functionclojure

In what way are Clojure maps functions?


In his talk "Maybe not" Rich Hickey states:

maps are (mathematical) functions!

in Clojure, we can directly write, and invoke

({:a 1 :b 2} :b) => 2

However I have the feeling that they are not in fact first class Clojure functions, or are they?

I can call the map with a keyword, or the other way around:

user=> (:b {:a 1 :b 2 :c 3})
2
user=> ({:a 1 :b 2 :c 3} :b)
2

But I can't use apply either way it seems:

user=> (apply #(:b %) {:a 1 :b 2 :c 3})
ArityException Wrong number of args (3) passed to: user/eval1762/fn--1763  clojure.lang.AFn.throwArity (AFn.java:429)

user=> (apply #({:a 1 :b 2 :c 3} %) :b)
IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Keyword  clojure.lang.RT.seqFrom (RT.java:542)

And neither can I apply the keyword directly to the map:

user=> (apply {:a 1 :b 2 :c 3} :b)
IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Keyword  clojure.lang.RT.seqFrom (RT.java:542)

So are they functions only in the mathematical sense, or is there more to them in the sense of applying a keyword similar to a "normal" clojure function?


Solution

  • Maps are functions of keys, as in your first two examples. Maps implement IFn interface just like "regular" Clojure functions.

    The reason apply doesn't work in your examples is due to the "sequence" arguments being passed in the second position.

    (apply #(:b %) {:a 1 :b 2 :c 3})
    

    In that example, the map argument is being turned into a sequence of key/value vectors/tuples, so they can be applied to #(:b %) (which wouldn't work anyway because that anonymous function only takes one argument). This is how the map would look as it's being turned into a sequence and applied as arguments to the function:

    user=> (seq {:a 1 :b 2 :c 3})
    ([:a 1] [:b 2] [:c 3])
    

    This second example doesn't work because :b is not a sequence — it's a single keyword. This works though:

    user=> (apply {:a 1 :b 2 :c 3} [:b])
    2
    

    Note that calling a map-as-function with apply and a sequence of keywords doesn't really make practical sense though.