From fcc84a810c36684fe983ebe65cbe2daea839bdf3 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Fri, 12 May 2017 16:15:05 -0700 Subject: [PATCH] add `other-siblings` --- pollen/pagetree.rkt | 17 +++++++++++++++++ pollen/private/ts.rktd | 2 +- pollen/scribblings/pagetree.scrbl | 18 ++++++++++++++++-- 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/pollen/pagetree.rkt b/pollen/pagetree.rkt index 6b76c14..3bd7a97 100644 --- a/pollen/pagetree.rkt +++ b/pollen/pagetree.rkt @@ -175,6 +175,23 @@ (check-false (siblings 'invalid-key test-pagetree))) +(define+provide/contract (other-siblings pnish [pt-or-path (current-pagetree)]) + (((or/c #f pagenodeish?)) ((or/c pagetree? pathish?)) . ->* . (or/c #f pagenodes?)) + (define sibs (for/list ([sib (in-list (or (siblings pnish pt-or-path) empty))] + #:unless (eq? sib (->pagenode pnish))) + sib)) + (and (pair? sibs) sibs)) + +(module-test-external + (define test-pagetree `(pagetree-main foo bar (one (two three four)))) + (check-equal? (other-siblings 'one test-pagetree) '(foo bar)) + (check-equal? (other-siblings 'foo test-pagetree) '(bar one)) + (check-equal? (other-siblings 'two test-pagetree) #f) + (check-equal? (other-siblings 'three test-pagetree) '(four)) + (check-false (other-siblings #f test-pagetree)) + (check-false (other-siblings 'invalid-key test-pagetree))) + + ;; private helper function. ;; only takes pt as input. ;; used by `pagetree?` predicate, so can't use `pagetree?` contract. diff --git a/pollen/private/ts.rktd b/pollen/private/ts.rktd index 15a0458..9dd1ac2 100644 --- a/pollen/private/ts.rktd +++ b/pollen/private/ts.rktd @@ -1 +1 @@ -1494629838 +1494630905 diff --git a/pollen/scribblings/pagetree.scrbl b/pollen/scribblings/pagetree.scrbl index 0b2945d..79db6d5 100644 --- a/pollen/scribblings/pagetree.scrbl +++ b/pollen/scribblings/pagetree.scrbl @@ -352,15 +352,29 @@ Find the child pagenodes of @racket[_p] within @racket[_pagetree]. Return @racke [p (or/c #f pagenodeish?)] [pagetree (or/c pagetree? pathish?) (current-pagetree)]) (or/c #f pagenode?)] -Find the sibling pagenodes of @racket[_p] within @racket[_pagetree]. The list will include @racket[_p] itself. But the function will still return @racket[#f] if @racket[_pagetree] is @racket[#f]. +Find the sibling pagenodes of @racket[_p] within @racket[_pagetree]. The result includes @racket[_p] itself. But the function will still return @racket[#f] if @racket[_pagetree] is @racket[#f]. @examples[#:eval my-eval -(current-pagetree '(root (mama.html son.html daughter.html) uncle.html)) +(current-pagetree '(root (mama.html son.html daughter.html))) (siblings 'son.html) (siblings 'daughter.html) (siblings 'mama.html) ] +@defproc[ +(other-siblings +[p (or/c #f pagenodeish?)] +[pagetree (or/c pagetree? pathish?) (current-pagetree)]) +(or/c #f pagenode?)] +Like @racket[siblings], but the result does not include @racket[_p] itself. + +@examples[#:eval my-eval +(current-pagetree '(root (mama.html son.html daughter.html))) +(other-siblings 'son.html) +(other-siblings 'daughter.html) +(other-siblings 'mama.html) +] + @deftogether[(