aboutsummaryrefslogtreecommitdiff
path: root/libs/dict.neb
blob: 6547511ed66ee4935fed2419348eef42e43600dc (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
52
53
54
55
; dicts
(func dict () (list))

(func dict? (in)
	(def flag #true)
	(for-each in 
		(if (not (eq? (list-length _item_) 2))
			(redef flag #false)))
	flag)

(func has? (in key)
	(def has #false)
	(for-each in
		(if (eq? (first _item_) key )
				(redef has #true)))
	has)

(func get (d key)
	(if (and (dict? d) (has? d key))
        (block
            (def val 0)
            (for-each d 
                (if (eq? (first _item_) key)
                    (redef val (first (rest _item_)))))
        val)
    ; we should panic here
    ))

(func set (d key val)
    (if (dict? d)
        (block
            (def out (dict))
            (for-each d
                (if (eq? (first _item_) key)
                    (redef out (append out (list key val)))
                    (redef out (append out _item_))))
            (if (not (has? out key))
                (redef out (append out (list key val))))
            out)
    ; we should panic here
    ))

(func keys (d) (map first d))

(func drop (d key)
    (if (dict? d)
        (block
            (def out (dict))
            (for-each d
                (if (not (eq? (first _item_) key))
                    (redef out (append out _item_))))
        out)

    ; we should panic here
    ))