boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

004-alist.scm (1456B)


      1 ;; tests/cc-util/04-alist.scm — alist-ref / alist-ref/eq / alist-set /
      2 ;; alist-update.
      3 ;; Assumes cc/util.scm is loaded.
      4 
      5 ;; alist-ref uses byte-aware equality for string/bytevector keys.
      6 (define al1 (alist-set "k1" 1 (alist-set "k2" 2 '())))
      7 (if (= (alist-ref "k1" al1) 1)      0 (sys-exit 1))
      8 (if (= (alist-ref "k2" al1) 2)      0 (sys-exit 2))
      9 (if (alist-ref "k3" al1)            (sys-exit 3) 0)   ; missing -> #f
     10 (if (= (alist-ref (bytevector 107 49) al1) 1) 0 (sys-exit 11))
     11 
     12 ;; alist-set conses on the front (later entries shadow older ones via search order)
     13 (define al2 (alist-set "k1" 99 al1))
     14 (if (= (alist-ref "k1" al2) 99)     0 (sys-exit 4))
     15 
     16 ;; alist-ref/eq uses eq? — symbol keys
     17 (define al3 (alist-set 'foo 10 (alist-set 'bar 20 '())))
     18 (if (= (alist-ref/eq 'foo al3) 10)  0 (sys-exit 5))
     19 (if (= (alist-ref/eq 'bar al3) 20)  0 (sys-exit 6))
     20 (if (alist-ref/eq 'baz al3)         (sys-exit 7) 0)
     21 
     22 ;; alist-update: present case replaces value
     23 (define al4 (alist-update "k1" (lambda (v) (+ v 100)) al1))
     24 (if (= (alist-ref "k1" al4) 101)    0 (sys-exit 8))
     25 (if (= (alist-ref "k2" al4) 2)      0 (sys-exit 9))   ; other key untouched
     26 (define al4b (alist-update (bytevector 107 49) (lambda (v) (+ v 200)) al1))
     27 (if (= (alist-ref "k1" al4b) 201)   0 (sys-exit 12))
     28 
     29 ;; alist-update: missing key -> upsert with (f #f)
     30 (define al5 (alist-update "new" (lambda (v) (if v v 7)) '()))
     31 (if (= (alist-ref "new" al5) 7)     0 (sys-exit 10))
     32 
     33 (sys-exit 0)