Hi Alex,

> Most of all, a cell can be passed to functions that expect a variable
> 'var' argument, like 'set', 'inc', 'pop', and many more. This is also
> the reason for the property functions 'prop' and '::'.
>
>    : (put 'A 'counter 0)
>    -> 0
>
>    : (put 'A 'list (1 2 3 4))
>    -> (1 2 3 4)
>
>    : (with 'A (inc (:: counter)))
>    -> 1
>
>    : (pop (prop 'A 'list))   
>    -> 1
>
>    : (show 'A)            
>    A NIL
>       list (2 3 4)
>       counter 1
>    -> A

Isn't it just because it was implemented this way?  I mean these
functions could be implemented to behave the same way even if getl
returned a list of (property-name . property-value) which in this case
would be:

:(put 'A 'counter 0)
-> 0
: (put 'A 'list (1 2 3 4))
-> (1 2 3 4)
: (getl 'A)
-> (((1 2 3 4) . list) (0 . counter)) # currently
-> ((list . (1 2 3 4)) (counter . 0)) # I would expect this?

Or, how is the order inside pairs for getl/putl important to these
functions?

> Another reason is that a property list could be searched for a given
> value with 'assoc'.

I see, you think it is more useful to search for a given value and I
thought it was more useful to search for a given property (even though
there are other functions but they don't work with the list of
properties as a whole).

How and what for would you use assoc in your example?

What information would you get if you found that some property has a
given value and how/what for would you use this information?  Also,
values can be "random" from a programmer point of view as opposed to
keys (usually) and a few properties can have the same value; in that
case assoc fails to be useful:

: (assoc 0 '((0 . list) (0 . counter)))
-> (0 . list)


Sometimes a list of (key . value) is needed (e.g. a list of attributes
for xml function) and in those cases, if I use getl in my code, I need
to "swap" car and cdr values which is rather inconvenient.

Am I missing something?


I was thinking about XML data binding, representing XML using symbols
instead of lists and that was where getl surprized me.  It is not an
issue though, just unexpected surprice;-)

# convert xml "list" (as returned by xml function) to xml "symbol"
(de xml2sym (S X)
   (if S
      (let (E (car X) A (cadr X) B (cddr X) G (group B))
         (for H G
            (let E2 (car H)
               (if (<= (length (cdr H)) 1)
                  (for I (cdr H)
                     (if (<= (length (cdr I)) 1)
                        (put S E2 (cadr I))
                        (put S E2 (xml2sym (new) (cons E2 I)))))
                  (for I (cdr H)
                     (if (<= (length (cdr I)) 1)
                        (put S E2 (conc (get S E2) (list (cadr I))))
                        (put S E2 (conc (get S E2)
                                        (list (xml2sym (new) (cons E2 
I))))))))))
         S)
      (when X
         (let (R (new))
            (put R (car X) (xml2sym (new) X))
            R))))

# convert xml "symbol" to xml "list" (as consumed by xml function)
(de sym2xml (S)
   (let P (car (getl S))
      (_sym2xml (car P) (cdr P))))

(de _sym2xml (S E)
   (if (or (num? S) (not (getl S)))
      (list E NIL S)
      (let (N (get S '@@ns)
            C (filter '((X) (not (pat? (cdr X)))) (getl S))
            A (filter '((X) (and (pat? (cdr X)) (<> '@@ns (cdr X)))) (getl S)))
         (make
            (link
               (if N (intern (pack N ': E)) E)
               (reverse
                  (mapcar
                     '((X) (cons (intern (pack (cdr (chop (cdr X))))) (car X)))
                     A)))
            (for I (reverse C)
               (if (lst? (car I))
                  (for J (car I)
                     (link (_sym2xml J (cdr I))))
                  (link (_sym2xml (car I) (cdr I)))))))))

(de xload (F)
   (xml2sym NIL (in F (xml))))

(de xwrite (S)
   (xml? T) (xml (sym2xml S)))

(de xsave (F S)
   (out F (xwrite S)))

To see how it works, run something like:

(setq X (xload "my.xml"))
(show X) ...
(xsave "my2.xml" X)

The advantage of this approach is that I can use all those property
access functions instead of searching for elements & attributes in a
list.  Also, other useful stuff could be added to a symbol
representing an XML element, e.g. indices for accessing items of a
list hold by a property in a specific order.

Thanks,

Tomas
-- 
UNSUBSCRIBE: mailto:[EMAIL PROTECTED]

Reply via email to