quad-first-char and quad-last-char

main
Matthew Butterick 10 years ago
parent 7bd08a679a
commit 975c5b7d7c

@ -68,6 +68,11 @@
[else #f])]
[else #f]))
(: quad-append (Quad QuadListItem . -> . Quad))
(define (quad-append q new-item)
(quad (quad-name q) (quad-attrs q) (append (quad-list q) (list new-item))))
(provide gather-common-attrs)
(: gather-common-attrs ((Listof Quad) . -> . (U False (Listof QuadAttrPair))))

@ -58,3 +58,15 @@
(check-true (quad-ends-with? (box #f (box #f "foo")) "o"))
(check-true (quad-ends-with? (box #f (box #f (box #f (box #f (box #f "foo-"))))) "-"))
(check-equal? (quad-append (box #f "foo") "bar") (box #f "foo" "bar"))
(check-equal? (quad-append (box #f "foo") (box #f "bar")) (box #f "foo" (box #f "bar")))
(check-equal? (quad-last-char (box #f (box #f "foo") "food")) "d")
(check-equal? (quad-last-char (box #f (box #f "foo"))) "o")
(check-equal? (quad-last-char (box #f "foo")) "o")
(check-false (quad-last-char (box)))
(check-equal? (quad-first-char (box #f (box #f "foo") "bar")) "f")
(check-equal? (quad-first-char (box #f (box #f "foo") "bar")) "f")
(check-equal? (quad-first-char (box #f "foo")) "f")
(check-false (quad-first-char (box)))

@ -148,4 +148,29 @@
(: quad-attr-remove* (Quad QuadAttrKey * . -> . Quad))
(define (quad-attr-remove* q . ks)
(for/fold ([current-q q])([k (in-list ks)])
(quad-attr-remove current-q k)))
(quad-attr-remove current-q k)))
;; the last char of a quad
(provide quad-last-char)
(: quad-last-char (Quad . -> . (Option String)))
(define (quad-last-char q)
(define split-qs (split-quad q)) ; split makes it simple, but is it too expensive?
(if (or (empty? split-qs) (empty? (quad-list (last split-qs))))
#f
(let ([result((inst car QuadListItem QuadListItem) (quad-list (last split-qs)))])
(if (quad? result)
(error 'quad-last-char "last element is not a string: ~v" result)
result))))
;; the first char of a quad
(provide quad-first-char)
(: quad-first-char (Quad . -> . (Option String)))
(define (quad-first-char q)
(define split-qs (split-quad q)) ; explosion makes it simple, but is it too expensive?
(if (or (empty? split-qs) (empty? (quad-list (first split-qs))))
#f
(let ([result((inst car QuadListItem QuadListItem) (quad-list (first split-qs)))])
(if (quad? result)
(error 'quad-first-char "first element is not a string: ~v" result)
result))))
Loading…
Cancel
Save