diff options
| author | mryouse | 2022-08-03 01:54:10 +0000 |
|---|---|---|
| committer | mryouse | 2022-08-03 01:54:10 +0000 |
| commit | 13b6853c10a0f564a9b7a16d97e823f955428413 (patch) | |
| tree | 210c20d9a58ab4989b69de365a118094a9e11a9d | |
| parent | ac7a6c08ebba80c6c5d8b4d8ce79d06a6c86d9ee (diff) | |
tighter definition
| -rw-r--r-- | libs/dict.neb | 38 |
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) |
