diff options
| -rw-r--r-- | libs/dict.neb | 55 | 
1 files changed, 55 insertions, 0 deletions
| diff --git a/libs/dict.neb b/libs/dict.neb new file mode 100644 index 0000000..6547511 --- /dev/null +++ b/libs/dict.neb @@ -0,0 +1,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 +    )) | 
