prevent `split-metas` from consuming #f args

pull/111/head
Matthew Butterick 9 years ago
parent 6ce1a65869
commit 3765a57be4

@ -2,6 +2,7 @@
(provide (all-defined-out)) (provide (all-defined-out))
(define (split-metas tree meta-key) (define (split-metas tree meta-key)
(define missing-sym (gensym))
(define matches null) (define matches null)
(define (meta? x) ; meta has form (define-meta key value) (define (meta? x) ; meta has form (define-meta key value)
@ -10,17 +11,17 @@
(define rest (define rest
(let loop ([x (if (list? tree) tree (list tree))]) (let loop ([x (if (list? tree) tree (list tree))])
(cond (cond
[(meta? x) (set! matches (cons x matches)) #f] [(meta? x) (set! matches (cons x matches)) missing-sym]
[(list? x) (filter (λ(x) x) (map loop x))] [(list? x) (filter (λ(x) (not (eq? x missing-sym))) (map loop x))]
[else x]))) [else x])))
(values (apply hasheq (apply append (reverse (map cdr matches)))) rest)) (values (apply hasheq (apply append (reverse (map cdr matches)))) rest))
(module+ test (module+ test
(require rackunit) (require rackunit)
(let-values ([(metas rest) (split-metas '(root (div (define-meta foo "bar") "hi") "zim" (define-meta foo "boing") "zam") 'define-meta)]) (let-values ([(metas rest) (split-metas '(root (div #:kw #f (define-meta foo "bar") "hi") "zim" (define-meta foo "boing") "zam") 'define-meta)])
(check-equal? metas '#hasheq((foo . "boing"))) (check-equal? metas '#hasheq((foo . "boing")))
(check-equal? rest '(root (div "hi") "zim" "zam"))) (check-equal? rest '(root (div #:kw #f "hi") "zim" "zam")))
(let-values ([(metas rest) (split-metas '(root (define-meta dog "Roxy") (define-meta dog "Lex")) 'define-meta)]) (let-values ([(metas rest) (split-metas '(root (define-meta dog "Roxy") (define-meta dog "Lex")) 'define-meta)])
(check-equal? metas '#hasheq((dog . "Lex")))) (check-equal? metas '#hasheq((dog . "Lex"))))
(let-values ([(metas rest) (split-metas '(root (define-meta dog "Roxy") (div (define-meta dog "Lex"))) 'define-meta)]) (let-values ([(metas rest) (split-metas '(root (define-meta dog "Roxy") (div (define-meta dog "Lex"))) 'define-meta)])

@ -1 +1 @@
1455046915 1455065350

Loading…
Cancel
Save