RE: Counting Nodes

Subject: RE: Counting Nodes
From: "Maltby, David G" <david.g.maltby@xxxxxxxx>
Date: Fri, 25 Feb 2000 10:42:52 -0500
With Brandon's theory that non-element class nodes were being included in
the node-list returned by (siblings) proven correct, I have rewritten my
procedure to trap those nodes and not count them.  This procedure provides
the functionality I need.

(define (text-node-cnt #!optional (cur-text-node (current-node)))
  (let loop ( (text-siblings (siblings cur-text-node)) (cnt 0) )
    (cond
      ((node-list-empty? text-siblings) 
        cnt)
      ((node-list=? cur-text-node (node-list-first text-siblings))
        (+ cnt 1)) ;exit
      ((not (gi (node-list-first text-siblings)))
        (loop (node-list-rest text-siblings) cnt)) ;don't count it
      ((has-attribute? (normalize "type") (node-list-first text-siblings))
        (loop (node-list-rest text-siblings) cnt)) ;don't count it
      (else
        (loop (node-list-rest text-siblings) (+ cnt 1)))))) ;count it

Is there are better or more appropriate element class test of the node then
(gi)?
Is there efficiencies to be gained by naming (node-list-first text-siblings)
rather then doing the (node-list-first ) call three times?

I tested Matthias' elegant procedure:

(define (text-node-cnt #!optional (cur-text-node (current-node)))
  (node-list-length
     (node-list-filter
       (lambda (snl) (not (attribute-string "type" snl)))
       (preced cur-text-node))))

and it works as well as my first attempt (well, it's one off but that is
minor), however it is too elegant for me and I could not successfully modify
the filter to get rid of the (not (gi snl)) as well as the (not
(attribute-string "type" snl)).  It will be a good exercise for me when
deadlines are not so pressing.  I need to understand mapping a procedure
over a list better.

BTW, has anyone seen a tutorial or paper on modes?  I understand enough to
know that they are a good thing, but I don't understand enough to use them
effectively.

Regards, David


 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist


Current Thread