Question

How to get the value of map element by key?

#clojure #maps

Answer

There's the get function, and "maps are functions of their keys" [^1]

({:k "v"} :k)
; => "v"
({"k" "v"} "k")
; => "v"

and "Keywords implement IFn for invoke() of one argument (a map) with an optional second argument (a default value)" [^1]

(:k {:k "v"})
; => "v"

but...

("k" {"k" "v"})
; => ClassCastException java.lang.String cannot be cast to clojure.lang.IFn  user/eval1 (NO_SOURCE_FILE:1)

[^1]: Data Structures

Learn

Author

xavi

Reviews
Last
next

Students 1