Arc Forumnew | comments | leaders | submitlogin
2 points by thaddeus 5752 days ago | link | parent

In retrospect, after doing some reading, I like the keywords concept outlined in kenny's dsb function. Although or=, works and I will use for now... in the long run I can see how having the sequence of arguments forced leading to bugs.

If I have many function calls all using an enforced sequence and for whatever reason the original function is changed (ie an inserted argument in the sequence list) all the function calls would have to be changed to accomodate.

The keyword concept also makes the code much more readable for the function calls: something like:

    (def the-example ((o car "mustang") (o year "1969") (o color "black")) 
         (prn "Car: " car)
         (prn "Year: " year)
         (prn "Color: " color)
    )
thus...

    (the-example car: camaro color: red year: 1968 )
is not only much more readable, but also the fact that I switched the order wouldn't impact the integrity of the code.

Additionally or=, prevents one from passing in nil as a valid argument...

probably stuff everyone here knows, but just my two cents!

.... actually in thinking about it.... I wonder if it was an intentional decision by pg not to put this feature in or was it just not gotten to?....

anyway - have a good night all, and thanks for the replies.

T.



1 point by thaddeus 5744 days ago | link

So when I originally made this posting I was attempting to create a function that would create the style attributes within in an html div.

Since the style parameters/arguments are completely differrent for each of my divs, I found it a pain to keep doing this:

    (pr "<div " "id=")
	(write (string "divLeftBox"))  
	(pr "style=")
    (write (string "position: absolute; left: 1px; top:" 1 "px;" 
	(if (is border->toggle 1)(string "border: 1px solid #ff9000;"))
	 "width:" width "px; " "height: " height "px;"))
	(pr ">")
So I took a stab at writing a function, following, that would handle all the possible formats, but discovered the variances too ugly in coding without being able to handle the "inside optional arguments" for a function:

     (def genstyle-old (overflow position top right bottom left width width-uom height height-uom border-width border-color)
              (or= overflow 'auto)
	          (or= position 'absolute)
	          (or= top 0)
	          (or= right 0)
	          (or= bottom 0)
		      (or= left 0)
			  (or= width 100)
              (or= width-uom "%")
			  (or= height 100)
              (or= height-uom "%")
			  (or= border-width 1)
		      (= border-line-style 'solid)
			  (or= border-color "#ff9000")
              (string "overflow: " overflow ";" " position: " position ";"
                             " top: " top "px;" " right: " right "px;" " bottom: " bottom "px;" " left: " left "px;"
                             " width: " width width-uom ";" " height: " height height-uom ";" 
	                         " border: " border-width "px" " solid " border-color ";")
    )
Anyway that turned out too ugly and Kenny's dsb function was too complicated for me to figure out, so I went back and looked into how pg was doing these type of things... and came up with the following:

    (mac genfn args (genlist args))

    (def genlist (spec)
       `(tostring ,@(parg (pair spec)))
    )


    (def parg (options)
       (if (no options)
        nil
       (let ((opt val) . rest) options
            (cons (parg-it opt val)
                  (parg rest))))
    )

    (def parg-it (key val)
        `(aif ,val (pr " " ',key " " it ";"))
    )

this allows me to do this:

     (def test ()
         (= height 10) 
         (= width 20)
         (gentag div id "mydiv" style (genfn position: "absloute" overflow: "auto" width: (string width "px") height: (string height "px") padding: "2px 3px 2px 3px"))
     )
resulting in this:

    arc> (test)
    <div id="mydiv" style=" position: absloute; overflow: auto; width: 20px; height: 10px; padding: 2px 3px 2px 3px;">
* note the significance for me is that I can pull the style arguments from a table and then autogenerate the list to be passed into the genfn function based upon which attributes exist in the table; then all I would need to do is pass in the table name. The end result will be 1 function that generates all of my divs, table driven.

I figured there might be some people out there (like myself) trying to do the same thing. I don't know how to put things on "git", nor do I want to throw my "hacks" into a repository that's probably intended for better code. :)

T.

-----