aboutsummaryrefslogtreecommitdiff
path: root/libs/dict.neb
blob: eea91949a70922bcbb702b0da6e07fdf89ad0ee0 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(func set? (val)
    (and
        (list? val)
        (not (dups val))))

(func pair? :bool (val)
    (and
        (list? val)
        (eq? 2 (length val))))

(type :pair :{:any} pair?)

(func dups (lst :[:any])
    (def out (list))
    (not (empty? (drop-while lst
        (if (in? _item_ out)
            #false
            (block
                (redef out (append out _item_))
                #true))))))

; dicts
(func dict? (val)
    (and
        (list? val)  ; TODO should this be automatic?
        (reduce and (map pair? val) #true)
        (not (dups (map first val)))))

(type :dict :[:pair] 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)
    (last (first
        (drop-while d
            (not (eq? key (first _item_)))))))


(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))