Structs describe structured data and offer an optimized way to work with such data.
(def person (struct [:name :age]))
Values can be tested to see if they conform with the struct:
(has-keys? person {:name "Huge", :age 37})
(not (has-keys? person {}))
A special kind of optimized struct-maps can be created,
using struct-map
and to-struct-map
or using the struct as a function:
(def p1 (person :name "Hugo" :age 27))
(def p2 (person {:name "Tina" :age 31}))
Struct-maps support all standard map functions like [[assoc]] and [[get]], as well as [[transient]] and [[assoc!]].
But adding keys not defined in the record, or removing any key will
change a struct-map into a hash-map! That behaviour can be changed
using lock
and unlock
. Locked struct maps throw an error in
these cases.
You can test if something is an actual struct-map:
(struct-map? p1)
(not (struct-map? (dissoc p1 :name)))
Structs support some reflection at runtime:
(= person (struct-of p1))
(= [:name :age] (struct-keys person))
(struct? person)
Structs describe structured data and offer an optimized way to work with such data. ``` (def person (struct [:name :age])) ``` Values can be tested to see if they conform with the struct: ``` (has-keys? person {:name "Huge", :age 37}) (not (has-keys? person {})) ``` A special kind of optimized struct-maps can be created, using [[struct-map]] and [[to-struct-map]] or using the struct as a function: ``` (def p1 (person :name "Hugo" :age 27)) (def p2 (person {:name "Tina" :age 31})) ``` Struct-maps support all standard map functions like [[assoc]] and [[get]], as well as [[transient]] and [[assoc!]]. But adding keys not defined in the record, or removing any key will change a struct-map into a hash-map! That behaviour can be changed using [[lock]] and [[unlock]]. Locked struct maps throw an error in these cases. You can test if something is an actual struct-map: ``` (struct-map? p1) (not (struct-map? (dissoc p1 :name))) ``` Structs support some reflection at runtime: ``` (= person (struct-of p1)) (= [:name :age] (struct-keys person)) (struct? person) ```
(accessor struct key)
Returns an optimized accessor function for the value associated with the given key in a struct-map of the given struct. Note that unlike the key itself, the optimized function will throw an error if called with values that are not not instances of the given struct.
Returns an optimized accessor function for the value associated with the given key in a struct-map of the given struct. Note that unlike the key itself, the optimized function will throw an error if called with values that are not not instances of the given struct.
(constructor struct)
Returns an optimized positional constructor function for struct-maps of the given struct. The order of the arguments to the constructor must match that of the definition of the struct, with additional fields from extended structs first.
Returns an optimized positional constructor function for struct-maps of the given struct. The order of the arguments to the constructor must match that of the definition of the struct, with additional fields from extended structs first.
(has-keys? struct v)
Tests if v
is a map that contains at least the keys defined for struct
.
Tests if `v` is a map that contains at least the keys defined for `struct`.
(mutator struct key)
Returns an optimized mutator function for the value associated with the given key in a struct-map of the given struct. Note that unlike the key itself, the optimized function will throw an error if called with values that are not not instances of the given struct.
Returns an optimized mutator function for the value associated with the given key in a struct-map of the given struct. Note that unlike the key itself, the optimized function will throw an error if called with values that are not not instances of the given struct.
(mutator! struct key)
Returns an optimized mutator function for the value associated with the given key in a transient struct-map of the given struct. Note that unlike the key itself, the optimized function will throw an error if called with values that are not not instances of the given struct.
Returns an optimized mutator function for the value associated with the given key in a transient struct-map of the given struct. Note that unlike the key itself, the optimized function will throw an error if called with values that are not not instances of the given struct.
(struct keys)
Returns the description of a structure, i.e. maps with the given keys.
Returns the description of a structure, i.e. maps with the given keys.
(struct-map struct & keys-vals)
Returns a new struct map with the keys of the struct.
(struct-map (struct [field-1 ...]) field-1 42 ...)
Returns a new struct map with the keys of the struct. ``` (struct-map (struct [field-1 ...]) field-1 42 ...) ```
(struct-of m)
Returns the struct the given struct-map was created from.
Returns the struct the given struct-map was created from.
(struct? v)
Tests if v is a struct as returned by struct
.
Tests if v is a struct as returned by [[struct]].
(to-struct-map struct keys-vals)
Returns a new struct map with the keys of the struct, from a collection of key-value tuples.
(def T (struct [field-1 ...]))
(to-struct-map T {field-1 42})
Returns a new struct map with the keys of the struct, from a collection of key-value tuples. ``` (def T (struct [field-1 ...])) (to-struct-map T {field-1 42}) ```
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close