From 13b6853c10a0f564a9b7a16d97e823f955428413 Mon Sep 17 00:00:00 2001 From: mryouse Date: Wed, 3 Aug 2022 01:54:10 +0000 Subject: tighter definition --- libs/dict.neb | 38 ++++++++++++++++++++++++++++---------- 1 file 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) -- cgit v1.2.3