Re: Getl and maps behavior?
Hi Alex, Having list cells behave like variables (by referring to their CAR parts) is a very useful feature. Not only for properties, but also for other list structures. : (setq L (1 2 3 4 5 6 7 8 9)) - (1 2 3 4 5 6 7 8 9) : (nth L 6) - (6 7 8 9) : (inc (nth L 6) 100) - 106 : (set (nth L 9) 777) - 777 : L - (1 2 3 4 5 106 7 8 777) ohh, I see now! Thanks for your patience:-) # convert xml list (as returned by xml function) to xml symbol ... # convert xml symbol to xml list (as consumed by xml function) ... As I said, this might give surprising results if you by chance encounter symbols used somewhere else in the system. Try (getl 'select)! If I try to convert 'select to XML, I get an error: (setq X2 (new)) (put X2 'select 'select) (xwrite X2) ?xml version=1.0 encoding=utf-8? !? (getl S) (((@Obj . @X) . @Lst) (@ unify (- @X)) (@P box (cdr (- @Lst))) (@C box (let L (car (- @Lst)) (setq L (or (mapcan select L) (select (car L) T))) (cons NIL L L))) (_gen @Obj) (_sel)) -- Symbol expected ? which is completely fine because it is not a valid input for sym2xml. It is a bit like feeding the 'xml' function with invalid XML, just get an error. Well, I do not know how to convert (((@Obj . @X) . @Lst) (@ unify (- @X)) (@P box (cdr (- @Lst))) (@C box (let L (car (- @Lst)) (setq L (or (mapcan select L) (select (car L) T))) (cons NIL L L))) (_gen @Obj) (_sel)) to XML so an error seems quite reasonable. However, if I feed it with the example Henrik provided: (class +Product +Entity) (rel name (+Need +String)) (rel id (+Need +Number)) (rel descr (+String)) (setq Product (new '(+Product) 'name PC 'id 123)) (setq X (new)) (put X 'product Product) (xwrite X) I get an XML: ?xml version=1.0 encoding=utf-8? product namePC/name id123/id /product I think that Henrik is basically aiming for similar thing, except his output format is not XML but JSON. It always returns the _whole_ property list, and this may contain other irrelevant data (as, for example, also Pilog stores rules in symbol properties, and the debug environment file names and line numbers). I think that storing these irrelevant data (or rather specific purpose data) is the good thing about representing XML using symbols. I can easily add more helper properties to a symbol during a computation. It might be more efficient working with lists, but maybe not so convenient. This would need an example though which I currently don't have:-( Cheers, Tomas -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED]
Re: Getl and maps behavior?
Hi Tomas, Alex, why does getl return a list of (property-value . property-name) and not a list of (property-name . property-value)? There are some practical reasons for representing the elements of property lists in such a way. 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 Another reason is that a property list could be searched for a given value with 'assoc'. Cheers, - Alex -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED]
Re: Getl and maps behavior?
Hi Tomas, 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 Ah, there is a misunderstanding. I was not talking especially about 'getl', but about how properties are implemented in general. It is important, IMHO, that they are of the form (value . key) internally. 'getl' simply returns the property list as it is. Surely it would have been possible to have 'getl' return a list of (key . value) pairs, but this would have involved an additional level of 'cons'ing, creating unnecessary garbage. :(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? If you use those functions directly on the result of 'getl', yes. But this was not the intention, and I think I never did it. The interesting point is to apply these functions directly to the cells of a property list as returned by 'prop' or '::', as described previously as (inc (:: counter)) or (pop (:: list)). Having list cells behave like variables (by referring to their CAR parts) is a very useful feature. Not only for properties, but also for other list structures. : (setq L (1 2 3 4 5 6 7 8 9)) - (1 2 3 4 5 6 7 8 9) : (nth L 6) - (6 7 8 9) : (inc (nth L 6) 100) - 106 : (set (nth L 9) 777) - 777 : L - (1 2 3 4 5 106 7 8 777) 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 This is available anyway, with 'get', ':' etc. How and what for would you use assoc in your example? In fact, I never needed that ;-) But for the matter of an example: : (mapc '((Key Val) (put 'A Key Val)) '(a b c d e f g) (1 2 3 4 5 6 7)) - 7 : (show 'A) A NIL g 7 f 6 e 5 d 4 c 3 b 2 a 1 - A : (get 'A 'd) - 4 : (assoc 4 (getl 'A)) - (4 . d) 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. I think 'getl' is not as important or useful as you seem to assume. It always returns the _whole_ property list, and this may contain other irrelevant data (as, for example, also Pilog stores rules in symbol properties, and the debug environment file names and line numbers). 'getl' was primarily intended as the counterpart to 'putl', allowing a fast cloning of symbols, or aid in debugging. For application-relevant data it is much more common to use explicit association lists. Assoc-lists were also the motivation to use the above structure for xml attributes. They are more flexible than property lists, because you can control aspects like the order of their elements, or how you search them (with 'assoc', 'asoq', 'find' etc). Property lists change their order on a last-recently used schema. 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;-) I would say it is best to use symbols when you have well defined, independent properties, and association lists when you need a whole list for a given operation. # convert xml list (as returned by xml function) to xml symbol ... # convert xml symbol to xml list (as consumed by xml function) ... As I said, this might give surprising results if you by chance encounter symbols used somewhere else in the system. Try (getl 'select)! The advantage of this approach is that I can use all those property access functions instead of searching for elements attributes in a Searching with 'assoc' or 'asoq' is about as convenient as using 'put' and 'get'. In addition, you can use the full set of list manipulation functions on association lists, while with property lists you are limited to the 'get' and 'put' function family, or need the overhead of 'getl' and 'putl'. This overhead is relatively large, as the whole property list needs to be copied. 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. Here, too, I believe that (nested) lists structures are more powerful. But it all depends, of course. It is always worth experimenting around a little. Cheers, - Alex -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED]
Re: Getl and maps behavior?
Hi Henrik, The main problem is that I need to be able to handle an arbitrary object and convert all Ts to true, and NILs to false in the JSON string. With the NILs we run into a problem, because a property value of NIL means that this property does not exist. So it can never be extracted from the object itself. To cover non-NIL properties, you could do (mapcar '((X) (or (pair X) (cons T X))) (getl Tst) ) Is there some other function/mechanism I could use instead? Hmm, not that I can think of at the moment. To solve the problem with the NILs, you'll have to keep a separate record of possible properties (as is done, for example, in the '+Entity' objects). Cheers, - Alex -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED]
Re: Getl and maps behavior?
Maybe this is a non-issue when I think about it. I can think of two possibilities: 1) For some reason an +Entity object needs to be converted to JSON. 2) A key = value structure needs to be sent, in such a case a paired list would do the trick, ie: ((k1 . v1) (k2 v2)) = {k1: v1, k2: v2}. This is probably more preferable than an object when I think even more :-). So, I'll implement that and take a look at converting entity objects. /Henrik On Thu, Aug 21, 2008 at 3:02 PM, Alexander Burger [EMAIL PROTECTED]wrote: Hi Henrik, The main problem is that I need to be able to handle an arbitrary object and convert all Ts to true, and NILs to false in the JSON string. With the NILs we run into a problem, because a property value of NIL means that this property does not exist. So it can never be extracted from the object itself. To cover non-NIL properties, you could do (mapcar '((X) (or (pair X) (cons T X))) (getl Tst) ) Is there some other function/mechanism I could use instead? Hmm, not that I can think of at the moment. To solve the problem with the NILs, you'll have to keep a separate record of possible properties (as is done, for example, in the '+Entity' objects). Cheers, - Alex -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED] --=_Part_31067_20121356.1219308350562 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline div dir=ltrMaybe this is a non-issue when I think about it. I can think of two possibilities:brbr1) For some reason an +Entity object needs to be converted to JSON.brbr2) A key =gt; value structure needs to be sent, in such a case a paired list would do the trick, ie: ((k1 . v1) (k2 quot;v2quot;)) =gt; {quot;k1quot;: v1, quot;k2quot;: quot;v2quot;}. This is probably more preferable than an object when I think even more :-).br brSo, I#39;ll implement that and take a look at converting entity objects.brbr/Henrikbrbrdiv class=gmail_quoteOn Thu, Aug 21, 2008 at 3:02 PM, Alexander Burger span dir=ltrlt;a href=mailto:[EMAIL PROTECTED][EMAIL PROTECTED]/agt;/span wrote:br blockquote class=gmail_quote style=border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;Hi Henrik,br div class=Ih2E3dbr gt; The main problem is that I need to be able to handle an arbitrary object andbr gt; convert all Ts to true, and NILs to false in the JSON string.br br /divWith the NILs we run into a problem, because a property value of NILbr means that this property does not exist. So it can never be extractedbr from the object itself.br br To cover non-NIL properties, you could dobr br nbsp; (mapcarbr nbsp; nbsp; nbsp;#39;((X) (or (pair X) (cons T X)))br nbsp; nbsp; nbsp;(getl Tst) )br div class=Ih2E3dbr br gt; Is there some other function/mechanism I could use instead?br br /divHmm, not that I can think of at the moment.br br To solve the problem with the NILs, you#39;ll have to keep a separatebr record of possible properties (as is done, for example, in the #39;+Entity#39;br objects).br br Cheers,br - Alexbr font color=#88--br UNSUBSCRIBE: mailto:a href=mailto:picolisp@software-lab.de;picolisp@software-lab.de/a?subject=Unsubscribebr /font/blockquote/divbr/div --=_Part_31067_20121356.1219308350562-- -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED] ---
Re: Getl and maps behavior?
Hi Henrik, convert all Ts to true, and NILs to false in the JSON string. With the NILs we run into a problem, because a property value of NIL means that this property does not exist. So it can never be extracted from the object itself. maybe the js - picolisp mapping false - NIL is not the right thing to do here? There are things like undefined, null, (empty string), NaN (maybe too?) or false you could represent as NIL but then you lose information during conversion and cannot rebuild the js data back again exactly. To solve the problem with the NILs, you'll have to keep a separate record of possible properties (as is done, for example, in the '+Entity' objects). Or, you could create your own unique NIL which would be treated by picolisp as any other non-NIL value and interpret it yourself during picolisp - javascript conversion as false, for example. : (setq MyNIL (new)) (setq Tst (new)) (put Tst 'a hello) (put Tst 'b T) (put Tst 'c MyNIL) # swap false, null, undefined or even NIL for MyNIL (getl Tst) (mapcar '((X) (or (pair X) (cons T X))) (getl Tst) ) (prog (prin [) (for (I . X) (mapcar '((X) (or (pair X) (cons T X))) (getl Tst) ) (when ( 1 I) (prin , )) (prin (cdr X) : ) (cond ((== (car X) MyNIL) (prin false)) ((== (car X) T) (prin true)) (T (print (car X) (prin ])) - $519715527 : - $519715537 : - hello : - T : - $519715527 : - (($519715527 . c) b (hello . a)) : - (($519715527 . c) (T . b) (hello . a)) : [c: false, b: true, a: hello]- ] This is oversimplified version as there are lots of special cases in javascript to handle, e.g. you have to double-quote keys if they are negative numbers if I remember well: [-1: negative numbers as strings in key] Alex, why does getl return a list of (property-value . property-name) and not a list of (property-name . property-value)? Cheers, Tomas -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED]
Re: Getl and maps behavior?
Thanks for the input Thomas but I think I will just go with paired lists instead, they are easier to manipulate with various list functions and should therefore be preferable to basic objects anyway. +Entity objects are on the list though. This is oversimplified version as there are lots of special cases in javascript to handle, e.g. you have to double-quote keys if they are negative numbers if I remember well: As far as I've been able to see any kind of key is double quoted in the json string (so that is what happens now), at least in the examples I've looked at. Now the question is, should we do basic objects when converting from json or paired lists. Basic objects are possible in that direction but that would then break with the rule of only accepting +Entity objects and paired lists for encoding. I don't know how a paired list vs a basic object compare when it comes to populating an +Entity object, any suggestions Alex? If an object is easier then that could motivate the decision to go for object when decoding and thus having different input and output formats. /Henrik On Fri, Aug 22, 2008 at 2:24 AM, Tomas Hlavaty [EMAIL PROTECTED] wrote: Hi Henrik, convert all Ts to true, and NILs to false in the JSON string. With the NILs we run into a problem, because a property value of NIL means that this property does not exist. So it can never be extracted from the object itself. maybe the js - picolisp mapping false - NIL is not the right thing to do here? There are things like undefined, null, (empty string), NaN (maybe too?) or false you could represent as NIL but then you lose information during conversion and cannot rebuild the js data back again exactly. To solve the problem with the NILs, you'll have to keep a separate record of possible properties (as is done, for example, in the '+Entity' objects). Or, you could create your own unique NIL which would be treated by picolisp as any other non-NIL value and interpret it yourself during picolisp - javascript conversion as false, for example. : (setq MyNIL (new)) (setq Tst (new)) (put Tst 'a hello) (put Tst 'b T) (put Tst 'c MyNIL) # swap false, null, undefined or even NIL for MyNIL (getl Tst) (mapcar '((X) (or (pair X) (cons T X))) (getl Tst) ) (prog (prin [) (for (I . X) (mapcar '((X) (or (pair X) (cons T X))) (getl Tst) ) (when ( 1 I) (prin , )) (prin (cdr X) : ) (cond ((== (car X) MyNIL) (prin false)) ((== (car X) T) (prin true)) (T (print (car X) (prin ])) - $519715527 : - $519715537 : - hello : - T : - $519715527 : - (($519715527 . c) b (hello . a)) : - (($519715527 . c) (T . b) (hello . a)) : [c: false, b: true, a: hello]- ] This is oversimplified version as there are lots of special cases in javascript to handle, e.g. you have to double-quote keys if they are negative numbers if I remember well: [-1: negative numbers as strings in key] Alex, why does getl return a list of (property-value . property-name) and not a list of (property-name . property-value)? Cheers, Tomas -- UNSUBSCRIBE: mailto:[EMAIL PROTECTED] --=_Part_44266_24757689.1219373194560 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline div dir=ltrbrThanks for the input Thomas but I think I will just go with paired lists instead, they are easier to manipulate with various list functions and should therefore be preferable to basic objects anyway. +Entity objects are on the list though.br brgt;gt; This is oversimplified version as there are lots of special cases inbr javascript to handle, e.g. you have to double-quote keys if they arebr negative numbers if I remember well:lt;lt;brbrAs far as I#39;ve been able to see any kind of key is double quoted in the json string (so that is what happens now), at least in the examples I#39;ve looked at.brbr Now the question is, should we do basic objects when converting from json or paired lists. Basic objects are possible in that direction but that would then quot;breakquot; with the rule of only accepting +Entity objects and paired lists for encoding. br brI don#39;t know how a paired list vs a basic object compare when it comes to populating an +Entity object, any suggestions Alex? If an object is easier then that could motivate the decision to go for object when decoding and thus having different input and output formats.br br/Henrikbrbrdiv class=gmail_quoteOn Fri, Aug 22, 2008 at 2:24 AM, Tomas Hlavaty span dir=ltrlt;a href=mailto:[EMAIL PROTECTED][EMAIL PROTECTED]/agt;/span wrote:brblockquote class=gmail_quote style=border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex; Hi Henrik,br div class=Ih2E3dbr gt;gt; convert all Ts to true, and NILs to false in the JSON string.br br gt; With the NILs we run into a problem, because a property value of NILbr gt; means that this property does not exist. So it can