simplifying following-sibling and preceding-sibling

"Oleg A. Paraschenko" <[email protected]> Thu, 13 Jan 2005 10:55:15 +0300
Newsgroups gmane.lisp.scheme.ssax-sxml
Organization xmlhack.ru
Message-ID <[email protected]>
  Hello,

  the code for "sxml:following-sibling" and "sxml:preceding-sibling"
contains a tricky enough code for tree traversal. I think these functions
can be simplified by dropping the traversal code and using "sxml:parent".
My version:

(define (sxml:following-sibling test-pred?)
  (lambda (root-node)   ; node or nodeset
    (lambda (node)   ; node or nodeset
      (if (nodeset? node)
        (map-union ((sxml:following-sibling test-pred?) root-node) node)
        ; Get all siblings which are the kids of the parent,
        ; find the node position and filter the rest
        (let* (
            (parent  (((sxml:parent sxml:node?) root-node) node))
            (kids    ((sxml:child  sxml:node?) parent))
            (inkids  (memq node kids)))
          (if inkids
            ((sxml:filter test-pred?) (cdr inkids))
            '()))))))


(define (sxml:preceding-sibling test-pred?)
  (lambda (root-node)   ; node or nodeset
    (lambda (node)   ; node or nodeset
      (if (nodeset? node)
        (map-union ((sxml:preceding-sibling test-pred?) root-node) node)
        ; Get all siblings which are the kids of the parent,
        ; find the node position and filter the previous nodes
        (let* (
            (parent  (((sxml:parent sxml:node?) root-node) node))
            (kids    ((sxml:child  sxml:node?) parent))
            (inkids  (memq node (reverse kids))))
          (if inkids
            (reverse ((sxml:filter test-pred?) (cdr inkids)))
            '()))))))


  Your comments are welcome.

-- 
Oleg



-------------------------------------------------------
The SF.Net email is sponsored by: Beat the post-holiday blues
Get a FREE limited edition SourceForge.net t-shirt from ThinkGeek.
It's fun and FREE -- well, almost....http://www.thinkgeek.com/sfshirt