4.13 Records
(require rebellion/collection/record) | |
package: rebellion |
A record is a collection of name-value mappings, each which is called a record field. The name of a field is a keyword. Records support constant-time lookup of field values by name.
Records are similar to hash tables, except keys must be keywords. Records are less dynamic than general-purpose hash tables, but their specialized nature can offer improved performance. In particular, constructing a record with record does not require sorting keywords at runtime, and calling a keyword-accepting function with a record imposes only constant-time overhead. Use records instead of hash tables when keys are expected to be literal names written in source code. As a rule of thumb, if you find yourself reaching for a hash table whose keys are symbols or strings, use records instead.
> (record #:name "Alyssa P. Hacker" #:age 42 #:favorite-color 'turqoise) (record #:age 42 #:favorite-color 'turqoise #:name "Alyssa P. Hacker")
procedure
(record-keywords rec) → keyset?
rec : record?
> (define rec (record #:name "Alyssa P. Hacker" #:age 42 #:favorite-color 'turqoise)) > (record-keywords rec) (keyset #:age #:favorite-color #:name)
procedure
(record-values rec) → immutable-vector?
rec : record?
> (define rec (record #:name "Alyssa P. Hacker" #:age 42 #:favorite-color 'turqoise)) > (record-values rec) '#(42 turqoise "Alyssa P. Hacker")
value
procedure
(record-size rec) → natural?
rec : record?
> (define rec (record #:name "Alyssa P. Hacker" #:age 42 #:favorite-color 'turqoise)) > (record-size rec) 3
procedure
(record-ref rec kw) → any/c
rec : record? kw : keyword?
> (define rec (record #:name "Alyssa P. Hacker" #:age 42 #:favorite-color 'turqoise)) > (record-ref rec '#:name) "Alyssa P. Hacker"
> (record-ref rec '#:fur-color) #f
procedure
(record-remove rec kw) → record?
rec : record? kw : keyword?
> (record-remove (record #:x 42 #:y 7) '#:x) (record #:y 7)
> (build-record keyword->string (keyset #:x #:y #:z)) (record #:x "x" #:y "y" #:z "z")
procedure
(record-merge2 rec1 rec2 [#:merge merge]) → record?
rec1 : record? rec2 : record? merge : (-> any/c any/c any/c) = (λ (a b) b)
> (record-merge2 (record #:x 1 #:y 2) (record #:name "Alyssa P. Hacker" #:age 42)) (record #:age 42 #:name "Alyssa P. Hacker" #:x 1 #:y 2)
> (record-merge2 (record #:x 1 #:y 2 #:z 3) (record #:z 100)) (record #:x 1 #:y 2 #:z 100)
> (record-merge2 (record #:x 1 #:y 2 #:z 3) (record #:x -1 #:y -2 #:z -3) #:merge +) (record #:x 0 #:y 0 #:z 0)
> (record-map (record #:x 1 #:y 2 #:z 3) (λ (x) (* x 100))) (record #:x 100 #:y 200 #:z 300)
procedure
(record-contains-key? rec kw) → boolean?
rec : record? kw : keyword?
> (record-contains-key? (record #:x 0 #:y 0) '#:x) #t
> (record-contains-key? (record #:x 0 #:y 0) '#:theta) #f
4.13.1 Record Fields
procedure
(record-field? v) → boolean?
v : any/c
procedure
(record-field #:<kw> v) → record-field?
v : any/c
> (record-field #:title "Fabulous Widget 2.0") (record-field #:title "Fabulous Widget 2.0")
> (record-field #:color 'firetruck-red) (record-field #:color 'firetruck-red)
procedure
(record-field-name field) → keyword?
field : record-field?
procedure
(record-field-value field) → any/c
field : record-field?
> (define field (record-field #:title "Fabulous Widget 2.0")) > (record-field-name field) '#:title
> (record-field-value field) "Fabulous Widget 2.0"