Records distinctly define compound data.
(def-record Person [person-name person-age])
Instances of the record can be created using the record as a function:
(def p1 (Person person-name "Hugo" person-age 27))
(def p2 (Person {person-name "Tina" person-age 31}))
Access and modification can be done using the record keys as a function:
(= 27 (person-age p1))
(= 45 (person-age (person-age p2 45)))
Values can be tested to see if they are instances of a record
(is-a? Person p1)
(not (is-a? Person :foo))
Record instances support the standard map functions, using the defined keys:
(update p1 person-age inc)
As well as destructuring like maps:
(let [{p1-age person-age, p1-name person-name} p1]
...)
But trying to add keys not defined in the record, or removing any keys will result in an error!
(assoc p1 :foo :bar) ;; -> error
(dissoc p1 person-age) ;; -> error
Record instances have a transient variant. Use the standard [[assoc!]] to modify them:
(persistent! (assoc! (transient p1) person-name "Sam"))
Records support some reflection at runtime:
(= Person (record-of p1))
(= [person-name person-age] (record-keys Person))
(= 'user/Person (record-name Person))
(record? Person)
Unlike clojures [[defrecord]], these records don't define a new type
in the host language. That has the disadvantage that you cannot
implement protocols for these records, but has the advantage that
records are first class values and are non-generative. That means
redefining a record (with the same keys) still defines the same
record and keys, that still work on instances of the previous
definition.
Records distinctly define compound data. ``` (def-record Person [person-name person-age]) ``` Instances of the record can be created using the record as a function: ``` (def p1 (Person person-name "Hugo" person-age 27)) (def p2 (Person {person-name "Tina" person-age 31})) ``` Access and modification can be done using the record keys as a function: ``` (= 27 (person-age p1)) (= 45 (person-age (person-age p2 45))) ``` Values can be tested to see if they are instances of a record ``` (is-a? Person p1) (not (is-a? Person :foo)) ``` Record instances support the standard map functions, using the defined keys: ``` (update p1 person-age inc) ``` As well as destructuring like maps: ``` (let [{p1-age person-age, p1-name person-name} p1] ...) ``` But trying to add keys not defined in the record, or removing any keys will result in an error! ``` (assoc p1 :foo :bar) ;; -> error (dissoc p1 person-age) ;; -> error ``` Record instances have a transient variant. Use the standard [[assoc!]] to modify them: ``` (persistent! (assoc! (transient p1) person-name "Sam")) ``` Records support some reflection at runtime: ``` (= Person (record-of p1)) (= [person-name person-age] (record-keys Person)) (= 'user/Person (record-name Person)) (record? Person) Unlike clojures [[defrecord]], these records don't define a new type in the host language. That has the disadvantage that you cannot implement protocols for these records, but has the advantage that records are first class values and are non-generative. That means redefining a record (with the same keys) still defines the same record and keys, that still work on instances of the previous definition. ```
(def-record t & args)
Defines a record and its keys, optionally giving a realm for each key:
(def-record T
[f1 :- realm/int,
f2 :- realm/string])
A corresponding instance can be created by using the record as a function:
(T f1 42 f2 "foo")
(T {f1 42 f2 "foo"})
Fields can be accessed and modified using the keys as functions:
(= 42 (f1 (T f1 42 f2 "foo")))
(= (T f1 42 f2 "bar") (f2 (T f1 42 f2 "foo") "bar"))
Defines a record and its keys, optionally giving a realm for each key: ``` (def-record T [f1 :- realm/int, f2 :- realm/string]) ``` A corresponding instance can be created by using the record as a function: ``` (T f1 42 f2 "foo") (T {f1 42 f2 "foo"}) ``` Fields can be accessed and modified using the keys as functions: ``` (= 42 (f1 (T f1 42 f2 "foo"))) (= (T f1 42 f2 "bar") (f2 (T f1 42 f2 "foo") "bar")) ```
(is-a? t v)
Tests if v
is an instance of the given record t
, or an extension of it.
Tests if `v` is an instance of the given record `t`, or an extension of it.
(is-exactly-a? t v)
Tests if v
is an instance of the given record t
.
Tests if `v` is an instance of the given record `t`.
(is-extended-from? t v)
Tests if v
is an instance of an extension of the given record t
.
Tests if `v` is an instance of an extension of the given record `t`.
(mutator! record key)
Returns a slightly optimized mutator function for transient record instances.
So instead of (assoc! (transient (r r-key 42)) r-key v)
you can
(def-record r [r-key])
(def r-key! (mutator! r r-key))
(r-key! (transient (r r-key 42)) 21)
Returns a slightly optimized mutator function for transient record instances. So instead of `(assoc! (transient (r r-key 42)) r-key v)` you can ``` (def-record r [r-key]) (def r-key! (mutator! r r-key)) (r-key! (transient (r r-key 42)) 21) ```
(record-extends t)
Returns the record that the given record t
extends, or nil if it doesn't extend another record.
Returns the record that the given record `t` extends, or nil if it doesn't extend another record.
(record-keys t)
Returns the keys of a record as a vector, including those added via extension.
Note: use [[keys]] to get the keys of an instance of a particular record instead.
Returns the keys of a record as a vector, including those added via extension. Note: use [[keys]] to get the keys of an instance of a particular record instead.
(record-name t)
Returns the name of the given record as a namespaced symbol.
Returns the name of the given record as a namespaced symbol.
(record-of m)
Returns the record defined via def-record
for the given instance of it.
Returns the record defined via [[def-record]] for the given instance of it.
(record? v)
Tests if v
is a record, as defined via def-record
.
Note: use is-a?
to test for instances of a particular record instead.
Tests if `v` is a record, as defined via [[def-record]]. Note: use [[is-a?]] to test for instances of a particular record instead.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close