Namespace for constructing and using realms to describe the shape of data.
Also has contains?
for dispatch on realms.
It also defines, via the compile
function, a shorthand
notation for realms, that is available in most places a realm is expected,
i.e. in realm combinators and realm syntax.
Namespace for constructing and using realms to describe the shape of data. Also has [[contains?]] for dispatch on realms. It also defines, via the [[compile]] function, a shorthand notation for realms, that is available in most places a realm is expected, i.e. in realm combinators and realm syntax.
Forms for attaching realms to functions.
Forms for attaching realms to functions.
Predicates, selectors, and realms for processing realm values as input.
Predicates, selectors, and realms for processing realm values as input.
Translate realms to Schema's schemas.
Translate realms to Schema's schemas.
Validate value realm membership.
Currently implemented via Schema.
Validate value realm membership. Currently implemented via Schema.
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. ```
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) ```
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close