aboutsummaryrefslogtreecommitdiff
path: root/libs
diff options
context:
space:
mode:
Diffstat (limited to 'libs')
-rw-r--r--libs/dict.neb38
1 files changed, 28 insertions, 10 deletions
diff --git a/libs/dict.neb b/libs/dict.neb
index 7dc3dcc..eea9194 100644
--- a/libs/dict.neb
+++ b/libs/dict.neb
@@ -1,16 +1,32 @@
+(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?
- (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))))))
+ (reduce and (map pair? val) #true)
+ (not (dups (map first val)))))
-(type :dict :[:any] dict?)
+(type :dict :[:pair] dict?)
(func dict :dict () (list))
@@ -19,8 +35,10 @@
(filter (lambda (x) (eq? (first x) key)) in))))
(func get (d :dict key)
- (first (rest (first
- (filter (lambda (x) (eq? (first x) key)) d)))))
+ (last (first
+ (drop-while d
+ (not (eq? key (first _item_)))))))
+
(func set :dict (d :dict key val)
(if (has? d key)