4 ms·
Fixed link to that style guide entry: https://guide.clojure.style/#opt-commas-in-map-literals https://guide.clojure.style/#opt-commas-in-map-literals Per that
by roryokane 3y ago
Fixed link to that style guide entry: https://guide.clojure.style/#opt-commas-in-map-literals https://guide.clojure.style/#opt-commas-in-map-literals
Per that style guide, the above map should be formatted like this:
{:a 1
2 :bar
[1 2 3] :baz}
Most maps written in EDN have keys of a consistent type. A map whose keys are consistently keywords would look like this when formatted:
{:a 1
:bar "https://example.com/"
:baz [1 2 3]}
Or like this, when condensed to one line:
{:a 1, :bar "https://example.com/", :baz [1 2 3]}
The first map had keys of three different types: keyword `:a`, integer `2`, and vector `[1 2 3]`. Why would one want a format that supports maps with mixed-type keys? As a contrived example, mixed-type keys let you define a sparse 2D tile-based map for a game that is indexed by x and y coordinates:
{:default {:type :grass}
[0 1] {:type :npc-rival}
[2 3] {:type :wall}
[2 4] {:type :wall}
[100 -5] {:type :treasure, :contents :sword-of-slaying}}
HN comment formatting help: https://news.ycombinator.com/formatdoc https://news.ycombinator.com/formatdoc. I indented the above code by two spaces.