-
Notifications
You must be signed in to change notification settings - Fork 4
record
A record is an unordered set of key-value pairs. Record keys can be integers, runes, symbols, or texts. The constructor procedure requires keys to be quoted. The : macro quotes keys automatically, but evaluates values. The (record: ...) literal auto-quotes everything.
(record 'foo 1 (if false 'baz 'bar) (+ 1 2)) -> (record: foo 1 bar 3)
(: foo 1 bar (+ 1 2)) -> (record: foo 1 bar 3)
(record: foo 1 bar (+ 1 2)) -> (record: foo 1 bar (+ 1 2))(record? x) -> returns true if x is a record
x.type -> record
x.size -> number of pairs in the record
x.clone -> copy the record
x.to-bool -> false if the record is empty, true otherwise
(x.get key) -> returns the value pointed to by key. This can be shortened to x.key.
(def rec (: x 1 y 2))
(rec.get 'x) -> 1
rec.x -> 1(x.put key val) -> return a new record with key set to val
(x.set! key val) -> mutate the record in place, setting key to val
(x.rm key) -> return a record with key and its value removed
(x.del! key) -> mutate the record in place, removing key and its value
(x.has? key) -> true if x contains a pair with key, false otherwise
(x.apply args) -> allows records to be used in the head of a code list
(def rec (: x 1 y 2))
(rec 'size) -> 2x.keys -> return a list of the record's keys x.values -> return a list of the record's values x.pairs, x.to-list -> return a list of key-value pairs x.to-plist -> return a list of keywords and values
(def rec (: foo 2 bar 3))
rec.keys -> (foo bar)
rec.values -> (2 3)
rec.pairs -> ((pair: foo 2) (pair: bar 3))
rec.to-list -> ((pair: foo 2) (pair: bar 3))
rec.to-plist -> (foo: 2 bar: 3)(x.merge y) -> return a new record composed of the pairs of x and y. On name conflicts, y overrides x.
(def x (: foo 1 bar 2))
(def y (: bar 3 baz 4))
(x.merge y) -> (record: bar 3 foo 1 baz 4)(x.fold init proc) -> apply the procedure proc to successive pairs of the record, returning a single value
(x.map proc) -> execute proc for each pair of the record and return a new record with the returned pairs
(x.filter predicate) -> return a record of pairs for whom the predicate returned true
(def yor (: a 1 b 2 c 3 d 4 e 5))
(yor.fold
0
(proc (total kv)
(+ total kv.val))) -> 15
(yor.reduce
(% total 1)
(proc (total kv)
(pair 'total (* total.val kv.val)))) -> (pair: total 120)
(proc square-pair (p)
(pair p.key (* p.val p.val)))
(yor.map square-pair) -> (record: a 1 b 4 c 9 d 16 e 25)
(yor.filter
(proc (p)
(!= p.val 4))) -> (record: a 1 b 2 c 3 e 5)