A struct instance is a map-like Clojure value created from a struct definition. It contains the definition’s keys, assigning nil to omitted keys, while accepting additional key-value pairs. section 11.1.4
Create and use struct instances through these operations:
- Use create-struct or defstruct to define the required keys. section 11.1.4
- Use struct for positional values; missing values become nil, and excess values raise IllegalArgumentException. section 11.1.4
- Use struct-map for key-value initialization; defined keys default to nil when omitted. section 11.1.4
- Use accessor to build a function that retrieves one key from instances; dissoc cannot remove a defined key. section 11.1.4
Normalize waypoint maps
(defstruct waypoint :type :lat :lon :id) (def coordinates [{:lat "18.3112" :lon "3.1314" :id "XVA"}]) (defn to-waypoints [coords] (map #(apply struct-map waypoint (mapcat identity %)) coords)) (to-waypoints coordinates) ;; ({:type nil, :lat "18.3112", :lon "3.1314", :id "XVA"})
Listing 11.4. → Constrained Map, Guarded keys
(create-struct [& keys]) (defstruct [name & keys]) (struct-map [s & inits]) (struct [s & vals]) (accessor [s key])
Browse the compact API signatures for defining, constructing, and accessing struct instances. section 11.1.4
