typed-work
Matthew Butterick 10 years ago
parent 308b49cfb7
commit 7ac9431232

@ -37,7 +37,6 @@
(Any -> Txexpr) (Any -> Txexpr)
(define-syntax-rule (validate-txexpr-element-with-context e) (validate-txexpr-element e #:context x)) (define-syntax-rule (validate-txexpr-element-with-context e) (validate-txexpr-element e #:context x))
(define-syntax-rule (validate-txexpr-attrs-with-context e) (validate-txexpr-attrs e #:context x)) (define-syntax-rule (validate-txexpr-attrs-with-context e) (validate-txexpr-attrs e #:context x))
(if (match x (if (match x
[(list (? symbol?)) #t] [(list (? symbol?)) #t]
;; todo: fix this condition ;; todo: fix this condition
@ -54,6 +53,7 @@
((Symbol) (Txexpr-Attrs (Listof Txexpr-Element)) . ->* . Txexpr) ((Symbol) (Txexpr-Attrs (Listof Txexpr-Element)) . ->* . Txexpr)
(cast (cons tag (append (if (empty? attrs) empty (list attrs)) elements)) Txexpr)) (cast (cons tag (append (if (empty? attrs) empty (list attrs)) elements)) Txexpr))
(define/typed (txexpr->values x) (define/typed (txexpr->values x)
(Txexpr -> (values Txexpr-Tag Txexpr-Attrs Txexpr-Elements)) (Txexpr -> (values Txexpr-Tag Txexpr-Attrs Txexpr-Elements))
(match (match
@ -64,21 +64,25 @@
[else `(,(car x) ,null ,@(cdr x))]) [else `(,(car x) ,null ,@(cdr x))])
[(list tag attr content ...) (values tag (cast attr Txexpr-Attrs) (cast content Txexpr-Elements))])) [(list tag attr content ...) (values tag (cast attr Txexpr-Attrs) (cast content Txexpr-Elements))]))
(define/typed (txexpr->list x) (define/typed (txexpr->list x)
(Txexpr -> (List Txexpr-Tag Txexpr-Attrs Txexpr-Elements)) (Txexpr -> (List Txexpr-Tag Txexpr-Attrs Txexpr-Elements))
(define-values (tag attrs content) (txexpr->values x)) (define-values (tag attrs content) (txexpr->values x))
(list tag attrs content)) (list tag attrs content))
;; convenience functions to retrieve only one part of txexpr ;; convenience functions to retrieve only one part of txexpr
(define/typed (get-tag x) (define/typed (get-tag x)
(Txexpr -> Txexpr-Tag) (Txexpr -> Txexpr-Tag)
(car x)) (car x))
(define/typed (get-attrs x) (define/typed (get-attrs x)
(Txexpr -> Txexpr-Attrs) (Txexpr -> Txexpr-Attrs)
(define-values (tag attrs content) (txexpr->values x)) (define-values (tag attrs content) (txexpr->values x))
attrs) attrs)
(define/typed (get-elements x) (define/typed (get-elements x)
(Txexpr -> Txexpr-Elements) (Txexpr -> Txexpr-Elements)
(define-values (tag attrs elements) (txexpr->values x)) (define-values (tag attrs elements) (txexpr->values x))
@ -90,6 +94,7 @@
(Can-Be-Txexpr-Attr-Key -> Txexpr-Attr-Key) (Can-Be-Txexpr-Attr-Key -> Txexpr-Attr-Key)
(if (string? x) (string->symbol x) x)) (if (string? x) (string->symbol x) x))
(define/typed (->txexpr-attr-value x) (define/typed (->txexpr-attr-value x)
(Can-Be-Txexpr-Attr-Value -> Txexpr-Attr-Value) (Can-Be-Txexpr-Attr-Value -> Txexpr-Attr-Value)
(->string x)) (->string x))
@ -100,21 +105,20 @@
(if (symbol? x) (symbol->string x) x)) (if (symbol? x) (symbol->string x) x))
;; broken: needs flatten
(define/typed (attrs->hash . items-in) (define/typed (attrs->hash . items-in)
(Can-Be-Txexpr-Attr * -> Txexpr-Attr-Hash) (Can-Be-Txexpr-Attr * -> Txexpr-Attr-Hash)
;; can be liberal with input because they're all just nested key/value pairs ;; can be liberal with input because they're all just nested key/value pairs
;; but still need this function to make sure that 'foo and "foo" are treated as the same hash key ;; but still need this function to make sure that 'foo and "foo" are treated as the same hash key
(define items (reverse (define items (reverse
(for/fold ([items : (Listof (U Can-Be-Txexpr-Attr-Key Can-Be-Txexpr-Attr-Value)) null])([i (in-list items-in)]) (for/fold ([items : (Listof (U Can-Be-Txexpr-Attr-Key Can-Be-Txexpr-Attr-Value)) null])
(cond ([i (in-list items-in)])
[(txexpr-attr? i) (append i items)] (cond
[(txexpr-attrs? i) (map (λ([i : Txexpr-Attr]) (append i items)) i)] [(txexpr-attr? i) (append i items)]
[else (cons i items)])))) [(txexpr-attrs? i) (append (append* i) items)]
[else (cons i items)]))))
(define/typed (make-key-value-list items) (define/typed (make-key-value-list items)
(Txexpr-Attrs -> (Listof (Pairof Txexpr-Attr-Key Txexpr-Attr-Value))) ((Listof (U Can-Be-Txexpr-Attr-Key Can-Be-Txexpr-Attr-Value)) -> (Listof (Pairof Txexpr-Attr-Key Txexpr-Attr-Value)))
(if (null? items) (if (>= (length items) 2)
null null
(let ([key (->txexpr-attr-key (car items))] (let ([key (->txexpr-attr-key (car items))]
[value (->txexpr-attr-value (cadr items))] [value (->txexpr-attr-value (cadr items))]
@ -122,29 +126,20 @@
(cons (cons key value) (make-key-value-list rest))))) (cons (cons key value) (make-key-value-list rest)))))
(make-immutable-hash (make-key-value-list items))) (make-immutable-hash (make-key-value-list items)))
;; broken
#;(define/typed (hash->attrs hash) (define/typed (hash->attrs attr-hash)
(Txexpr-Attr-Hash -> Txexpr-Attrs) (Txexpr-Attr-Hash -> Txexpr-Attrs)
(hash-map hash list)) (for/list : Txexpr-Attrs ([(k v) (in-hash attr-hash)])
(list k v)))
;; broken. needs txexpr-attrs? filter to work (define/typed (attrs-have-key? x key)
#;(define/typed (attrs-have-key? x key)
((U Txexpr-Attrs Txexpr) Can-Be-Txexpr-Attr-Key -> Boolean) ((U Txexpr-Attrs Txexpr) Can-Be-Txexpr-Attr-Key -> Boolean)
(define attrs (if (txexpr-attrs? x) x (get-attrs x))) (define attrs (if (txexpr-attrs? x) x (get-attrs x)))
(hash-has-key? (attrs->hash attrs) (->txexpr-attr-key key))) (hash-has-key? (attrs->hash attrs) (->txexpr-attr-key key)))
;; broken. needs txexpr-attrs? filter to work
#;(define/typed (attrs-equal? x1 x2)
((U Txexpr-Attrs Txexpr) (U Txexpr-Attrs Txexpr) -> Boolean)
(define attrs-tx1 (attrs->hash (if (txexpr-attrs? x1) x1 (get-attrs x1))))
(define attrs-tx2 (attrs->hash (if (txexpr-attrs? x2) x2 (get-attrs x2))))
(and
(= (length (hash-keys attrs-tx1)) (length (hash-keys attrs-tx2)))
(for/and ([(key value) (in-hash attrs-tx1)])
(equal? (hash-ref attrs-tx2 key) value))))
;; broken. needs txexpr-attrs? filter to work (define/typed (attrs-equal? x1 x2)
#;(define/typed (attrs-equal? x1 x2)
((U Txexpr-Attrs Txexpr) (U Txexpr-Attrs Txexpr) -> Boolean) ((U Txexpr-Attrs Txexpr) (U Txexpr-Attrs Txexpr) -> Boolean)
(define attrs-tx1 (attrs->hash (if (txexpr-attrs? x1) x1 (get-attrs x1)))) (define attrs-tx1 (attrs->hash (if (txexpr-attrs? x1) x1 (get-attrs x1))))
(define attrs-tx2 (attrs->hash (if (txexpr-attrs? x2) x2 (get-attrs x2)))) (define attrs-tx2 (attrs->hash (if (txexpr-attrs? x2) x2 (get-attrs x2))))
@ -153,53 +148,48 @@
(for/and ([(key value) (in-hash attrs-tx1)]) (for/and ([(key value) (in-hash attrs-tx1)])
(equal? (hash-ref attrs-tx2 key) value)))) (equal? (hash-ref attrs-tx2 key) value))))
;; broken. needs hash->attrs
#;(define/typed (attr-set tx key value) (define/typed (attr-set tx key value)
(Txexpr Can-Be-Txexpr-Attr-Key Can-Be-Txexpr-Attr-Value -> Txexpr) (Txexpr Can-Be-Txexpr-Attr-Key Can-Be-Txexpr-Attr-Value -> Txexpr)
(define new-attrs (define new-attrs
(hash->attrs (hash-set (attrs->hash (get-attrs tx)) (->txexpr-attr-key key) (->txexpr-attr-value value)))) (hash->attrs (hash-set (attrs->hash (get-attrs tx)) (->txexpr-attr-key key) (->txexpr-attr-value value))))
(make-txexpr (get-tag tx) new-attrs (get-elements tx))) (make-txexpr (get-tag tx) new-attrs (get-elements tx)))
;; broken: needs attrs->hash (define/typed (attr-ref tx key)
#;(define/typed (attr-ref tx key)
(Txexpr Can-Be-Txexpr-Attr-Key -> Txexpr-Attr-Value) (Txexpr Can-Be-Txexpr-Attr-Key -> Txexpr-Attr-Value)
(with-handlers ([exn:fail? (λ(e) (error (format "attr-ref: no value found for key ~v" key)))]) (with-handlers ([exn:fail? (λ(e) (error (format "attr-ref: no value found for key ~v" key)))])
(hash-ref (attrs->hash (get-attrs tx)) key))) (hash-ref (attrs->hash (get-attrs tx)) (->txexpr-attr-key key))))
;; broken: needs attrs-have-key? (define/typed (attr-ref* tx key)
#;(define/typed (attr-ref* tx key)
(Txexpr Can-Be-Txexpr-Attr-Key -> (Listof Txexpr-Attr-Value)) (Txexpr Can-Be-Txexpr-Attr-Key -> (Listof Txexpr-Attr-Value))
(filter-not false? (define results : (Listof Txexpr-Attr-Value) empty)
(flatten (let loop : Void ([tx : Xexpr tx])
(let loop ([tx tx]) (when (and (txexpr? tx) (attrs-have-key? tx key) (attr-ref tx key))
(and (txexpr? tx) (set! results (cons (attr-ref tx key) results))
(cons (and (attrs-have-key? tx key)(attr-ref tx key)) (map (λ([e : Txexpr-Element]) (loop e)) (get-elements tx))
(map loop (get-elements tx)))))))) (void)))
results)
;; convert list of alternating keys & values to attr ;; convert list of alternating keys & values to attr
;; broken: needs attrs->hash (define/typed (merge-attrs . items)
#;(define/typed (merge-attrs . items)
(Txexpr-Attr * -> Txexpr-Attrs) (Txexpr-Attr * -> Txexpr-Attrs)
(define attrs-hash (apply attrs->hash items)) (define attrs-hash (apply attrs->hash items))
;; sort needed for predictable results for unit tests ;; sort needed for predictable results for unit tests
(define sorted-hash-keys (sort (hash-keys attrs-hash) (λ(a b) (string<? (->string a) (->string b))))) (define sorted-hash-keys (sort (hash-keys attrs-hash) (λ([a : Txexpr-Tag][b : Txexpr-Tag]) (string<? (->string a) (->string b)))))
`(,@(map (λ(key) (list key (hash-ref attrs-hash key))) sorted-hash-keys))) `(,@(map (λ([key : Txexpr-Tag]) (list key (hash-ref attrs-hash key))) sorted-hash-keys)))
;; broken
#;(define/typed (remove-attrs x)
(case-> (Xexpr -> Xexpr)
((Listof Xexpr) -> (Listof Xexpr))
((Listof Xexpr) -> Xexpr))
(cond
[(txexpr? x) (let-values ([(tag attr elements) (txexpr->values x)])
(make-txexpr tag null (remove-attrs elements)))]
[(txexpr-elements? x) (map remove-attrs x)]
[else x]))
#|
(define/typed (remove-attrs x)
(Xexpr -> Xexpr)
(if (txexpr? x)
(let-values ([(tag attr elements) (txexpr->values x)])
(make-txexpr tag null (map remove-attrs elements)))
x))
#|
;; todo: exclude-proc will keep things out, but is there a way to keep things in? ;; todo: exclude-proc will keep things out, but is there a way to keep things in?
(define+provide+safe (map-elements/exclude proc x exclude-test) (define+provide+safe (map-elements/exclude proc x exclude-test)

Loading…
Cancel
Save