Re: Nodes and counting

Subject: Re: Nodes and counting
From: christo@xxxxxxxxxxxxxxxxxx (Frank Christoph)
Date: Wed, 1 Oct 1997 00:08:58 +0900
> [ What I really want is an  (if-exist (sibling "child2") test - Does one
> exist? I.e. test for a sibling with
> given name, to make me less dependent on the dtd structure.

At the risk of making a mountain out of a molehill, how about this:

(define (or-map f #!rest bs)
  (apply or (map f bs))

(define (curry f x) ; N.B., arity(f) > 1
   (lambda (!#rest xs) (apply f (cons x xs))))

(define (gid=? x #!optional str)
 (cond
   ((and (node-list? x) (string? str))
      (string=? x str))
   ((string? x)
      (string=? (current-node) x))
   (else
      (error "gid=?: expected (gid=? [<node>] <string>)"))))

(let* ((pred (lambda (n) (or-map (curry gid=? n) "child2" "child3")))
       (ns   (node-list-filter pred (follow (current-node)))))
     (if (node-list-empty? ns) 0em 2em))

Here's a slightly shorter version, but I don't think Jade implements
node-list-some?. (Of course, you can always implement it yourself.  There's a
sample in the standard.):

(let ((pred (lambda (n) (or-map (curry gid=? n) "child2" "child3")))
    (if (node-list-some? pred (follow (current-node))) 0em 2em))

Remember, you have to be careful with or-map.  In a lazy language it's an
occasionally useful combinator, but in DSSSL the #!rest arguments will all
get evaluated no matter what they are.  Compare:

(define (undef) (undef))
(define (id x) x)

(or #t (undefined)) ==> #t
(or-map id #t (undefined)) ==> <doesn't terminate>

As long as you apply or-map only to constants you won't run into any trouble,
though.  

--FC

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


Current Thread