Skip to content
TurtleKitty edited this page May 18, 2019 · 3 revisions

table

A table 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 #(table ...) literal auto-quotes everything.

(table 'foo 1 (if false 'baz 'bar) (+ 1 2))
   ; #(table foo 1 bar 3)

(: foo 1 bar (+ 1 2))
   ; #(table foo 1 bar 3)

#(table foo 1 bar (+ 1 2))
   ; #(table foo 1 bar (+ 1 2))

predicate

(table? x) ; returns true if x is a table

messages

(def barren (table))
(def point (: x 1 y 2))

point.type  ; (table)

barren.size    ; 0
barren.to-bool ; false
barren.empty?  ; true

point.size     ; 2
point.to-bool  ; true
point.empty?   ; false

(point.get 'x) ; 1
point.x        ; 1
point.y        ; 2
point.z        ; null

(def p2 point.clone)
(= point p2)   ; true
(is? point p2) ; false

; put is functional

(point.put 'z 3)
   ; #(table x 1 y 2 z 3)

(point.has? 'z)   ; false
point.z           ; null

; set! is imperative

(point.set! 'z 3) ; 3

(point.has? 'z)   ; true
point.z           ; 3

; rm is functional

(point.rm 'z)
   ; #(table x 1 y 2)

(point.has? 'z)   ; true
point.z     ; 3

; del! is imperative

(point.del! 'z)

(point.has? 'z)   ; false
point.z           ; null

(point 'x)        ; 1 - tables answer the apply message

point.keys        ; (x y)
point.values      ; (1 2)
point.pairs       ; ((x . 1) (y . 2))

(def tx (: foo 1 bar 2))
(def ty (: bar 3 baz 4))

(tx.union ty) ; #(table bar 3 foo 1 baz 4)
Clone this wiki locally