Re: Calculating age based on DOB
Hi David, Essentially I can calculate age based off of DOB with a (dm T ...) line 18 but then that record won't show in the search dialog. When lines 13-20 are You are quite close :) The problem is the way your 'T' method is defined: (dm T (name NAME prefname PREFNAME sex SEX dob DOB email EMAIL txt TXT) (=: name NAME) ... This is not how it works. You could either define it as (dm T (NAME PREFNAME SEX DOB EMAIL TXT) (=: name NAME) ... and then call 'new!' (which is implied in the 'newButton' function): (new! '(+Person) *Name *PrefName ...) OR (and this is how I would do it), use the fact that the 'new' and 'new!' functions, and also the 'T' method of the '+Entity' parent class, accepts arguments as alternating keys and values: (dm T (DOB . @) (pass super) # Pass remaining arguments to the parent class (=: age (/ (- (date) DOB) 365)) ) or, directly, (dm T (DOB . @) (pass super 'age (/ (- (date) DOB) 365)) ) and then call it as (new! '(+Person) DOB 'name *Name 'prefname *PrefName ...) or (newButton T Dst '(+Person) DOB 'name *Name 'prefname *PrefName ...) BTW, a year has more than 365 days. A somewhat more accurate calculation might be (*/ (- (date) DOB) 100 36525)) Besides the fact, of course, that storing the _age_ in the database doesn't make sense, It is soon out of date ;-) Also, what is the idomatic way to include an outside .js file in head.../head? The best is to push the file name into the global '*JS' variable before program start: (push1 '*JS (allow myFile.js)) Examples for that are in lib/form.l or lib/canvas.l. ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Calculating age based on DOB
Thank you for your prompt and helpful reply Alex. The *JS trick worked great and the proper date calculation will be most helpful. If I may ask a few more questions: You're right, storing the age is silly when already storing DOB. How might I just calculate age on the fly and display it in the following (gui ...) or similar? (grid 6 ... Birthdate (gui '(+E/R +DateField) '(dob : home obj) 10 *DOB) Age (gui ...) ...) Aditionally, how would I go about including a .css file to override certain bits of the built-in one? Thanks again for all the help, I'm taking the plunge and using PicoLisp for my second non-trivial application (first one is in Common Lisp) and absolutely thrilled about it so far. The learning curve is indeed steep but the control and expressiveness gained once a better command of the language is obtained is nothing short of breathtaking. Warm regards, David Bloom On Sun, Jan 19, 2014 at 1:12 PM, Alexander Burger a...@software-lab.dewrote: Hi David, Essentially I can calculate age based off of DOB with a (dm T ...) line 18 but then that record won't show in the search dialog. When lines 13-20 are You are quite close :) The problem is the way your 'T' method is defined: (dm T (name NAME prefname PREFNAME sex SEX dob DOB email EMAIL txt TXT) (=: name NAME) ... This is not how it works. You could either define it as (dm T (NAME PREFNAME SEX DOB EMAIL TXT) (=: name NAME) ... and then call 'new!' (which is implied in the 'newButton' function): (new! '(+Person) *Name *PrefName ...) OR (and this is how I would do it), use the fact that the 'new' and 'new!' functions, and also the 'T' method of the '+Entity' parent class, accepts arguments as alternating keys and values: (dm T (DOB . @) (pass super) # Pass remaining arguments to the parent class (=: age (/ (- (date) DOB) 365)) ) or, directly, (dm T (DOB . @) (pass super 'age (/ (- (date) DOB) 365)) ) and then call it as (new! '(+Person) DOB 'name *Name 'prefname *PrefName ...) or (newButton T Dst '(+Person) DOB 'name *Name 'prefname *PrefName ...) BTW, a year has more than 365 days. A somewhat more accurate calculation might be (*/ (- (date) DOB) 100 36525)) Besides the fact, of course, that storing the _age_ in the database doesn't make sense, It is soon out of date ;-) Also, what is the idomatic way to include an outside .js file in head.../head? The best is to push the file name into the global '*JS' variable before program start: (push1 '*JS (allow myFile.js)) Examples for that are in lib/form.l or lib/canvas.l. ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe
Re: Calculating age based on DOB
Hi David, Thank you for your prompt and helpful reply Alex. The *JS trick worked great and the proper date calculation will be most helpful. If I may ask a One more thought: Perhaps a better way to calculate the age is to avoid floating points, and do it directly in integer calculation: (de age (Birth) (setq Birth (date Birth)) (let (Dat (date (date)) Age (- (car Dat) (car Birth))) (when ( (cdr Birth) (cdr Dat)) (dec 'Age) ) Age ) ) Here, the calls (cdr Birth) and (cdr Dat) each return a list of the form (month day), so they can be directly compared. few more questions: You're right, storing the age is silly when already storing DOB. How might I just calculate age on the fly and display it in the following (gui ...) or similar? (grid 6 ... Birthdate (gui '(+E/R +DateField) '(dob : home obj) 10 *DOB) Note: the '*DOB' at the end is notneeded. You might pass another label here, which is probably not what you want. I would use the '+View' prefix, for a read-only field. With the above 'age' function it gives: Age (gui '(+View +DateField) '(age (: home obj dob)) 10) Aditionally, how would I go about including a .css file to override certain bits of the built-in one? Instead of a single CSS file, you can also pass a list of files: (html ... '(@lib.css my/lib.css) ... As my/lib.css comes after @lib.css, it will override styles in @lib.css. ♪♫ Alex -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe