aboutsummaryrefslogtreecommitdiff
path: root/libs/dict.neb
blob: 7dc3dcc6adf053b71ffffe14d07069dd35bbffc6 (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
33
; dicts
(func dict? (val)
    (and
        (list? val)  ; TODO should this be automatic?
        (or
            (eq? 0 (length val))
            (and
                (eq? 1 (length val))
                (list? (first val)))
            (apply and (map list? val)))
        (eq? 0 (length (filter (lambda (x) (not (eq? x 2))) (map length val))))))

(type :dict :[:any] dict?)

(func dict :dict () (list))

(func has? :bool (in :dict key)
    (not (empty?
        (filter (lambda (x) (eq? (first x) key)) in))))

(func get (d :dict key)
    (first (rest (first
        (filter (lambda (x) (eq? (first x) key)) d)))))

(func set :dict (d :dict key val)
    (if (has? d key)
        (map (lambda (x) (if (eq? key (first x)) (list (first x) val) x)) d)
        (append d (list key val))))

(func keys (d :dict) (map first d))

(func drop (d :dict key)
    (filter (lambda (x) (not (eq? (first x) key))) d))