aboutsummaryrefslogtreecommitdiff
path: root/advent-of-code/2023/lib/obj.rkt
blob: 578ea36e03c83cae59fcc50fe3ce5145bced4289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#lang racket/base

(provide obj-maker
         obj-set
         obj-show)

(define (alist->obj alist)
  (lambda key
    (if (null? key)
      alist
      (cadr (assoc (car key) alist)))))

(define (obj-maker . fields)
  (lambda inits
    (define alist (map list fields inits))
    (alist->obj alist)))

(define (obj-set record key value)
  (define alist (record))
  (define new-alist
    (let loop ((new-list '()) (cur alist) (is-set #f))
      (if (null? cur)
        (if is-set
          new-list
          (cons (list key value) new-list))
        (let ()
          (if (eq? key (caar cur))
            (loop (cons (list key value) new-list) (cdr cur) #t)
            (loop (cons (car cur) new-list) (cdr cur) is-set))))))
  (alist->obj new-alist))

(define (obj-show x) (x))