Liking cljdoc? Tell your friends :D

active.data.struct

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)
```
raw docstring

accessorclj/s

(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.
sourceraw docstring

constructorclj/s

(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.
sourceraw docstring

has-keys?clj/s

(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`.
sourceraw docstring

lockclj/s

(lock struct-map)
source

mutatorclj/s

(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.
sourceraw docstring

mutator!clj/s

(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.
sourceraw docstring

structclj/s

(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.
sourceraw docstring

struct-keysclj/s

(struct-keys struct)
source

struct-mapclj/s

(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 ...)
```
sourceraw docstring

struct-map?clj/s

(struct-map? v)
source

struct-ofclj/s

(struct-of m)

Returns the struct the given struct-map was created from.

Returns the struct the given struct-map was created from.
sourceraw docstring

struct?clj/s

(struct? v)

Tests if v is a struct as returned by struct.

Tests if v is a struct as returned by [[struct]].
sourceraw docstring

to-struct-mapclj/s

(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})
```
sourceraw docstring

unlockclj/s

(unlock struct-map)
source

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close