aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
authormryouse2022-06-15 22:15:47 +0000
committermryouse2022-06-15 22:15:47 +0000
commit3b8834623d780316f81535029f2f8fee40736878 (patch)
tree28890740612aa6b5cf997168de2082119e8fa731 /libs
parentfff3bf748899f524bd24c1243497cfb6ca825c33 (diff)
dicts, but better
Diffstat (limited to 'libs')
-rw-r--r--libs/dict.neb51
1 files changed, 10 insertions, 41 deletions
diff --git a/libs/dict.neb b/libs/dict.neb
index 6547511..7b474f4 100644
--- a/libs/dict.neb
+++ b/libs/dict.neb
@@ -2,54 +2,23 @@
(func dict () (list))
(func dict? (in)
- (def flag #true)
- (for-each in
- (if (not (eq? (list-length _item_) 2))
- (redef flag #false)))
- flag)
+ (empty?
+ (filter (lambda (x) (not (eq? x 2))) (map list-length in))))
(func has? (in key)
- (def has #false)
- (for-each in
- (if (eq? (first _item_) key )
- (redef has #true)))
- has)
+ (not (empty?
+ (filter (lambda (x) (eq? (first x) key)) in))))
(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
- ))
+ (first (rest (first
+ (filter (lambda (x) (eq? (first x) key)) d)))))
(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
- ))
+ (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) (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
- ))
+ (filter (lambda (x) (not (eq? (first x) key))) d))