Optimizing 'deep' setf

Started by Tim Johnson, January 14, 2010, 05:50:43 PM

Previous topic - Next topic

Tim Johnson

I have written a function that modifies a list

four "levels" deep, or to put it another way, has four dimensions.

My method uses multiple calls to 'setf. I would welcome any comments

or advices as to how to optimize this function - if possible.

The preconditions are this:

I have a list that has as members (level 1) that may be either

strings or lists. Any list at level 1 may have as members either

lists or strings. (level 2)

Any list at level 2 may be nested 2 more levels deep.

What follows is

List at level 2 before modification

("form0"  ;; before
  ("type" "form")
  ("attrs" (
    ("ID" "form0")
    ("method" "POST")
    ("name" "form0")
    ("onsubmit" "return checkLoginForm(this);")
    ("action" "[[action]]")))) ;; Value referenced by "action"

List at level 2 after modification

("form0"   ;; after
  ("type" "form")
  ("attrs" (
    ("ID" "form0")
    ("method" "POST")
    ("name" "form0")
    ("onsubmit" "return checkLoginForm(this);")
    ("action" "http://localhost/cgi-bin/test.lsp"))))  ;; value changed

Now here we have the function

(define (set-form-action newaction (f 0))  ;; 'get-form sets 'current-formname
  (letn ((form-list(get-form f))           ;; level 1 referenced from 'DS by 'current-formname
         (form-tag (form-list 1))          ;; level 2
         (attrs(lookup "attrs" form-tag))  ;; level 3
         (action))
    (if (set 'action (assoc "action" attrs)) ;; level 4
      (begin
        (setf (action 1) newaction             ;; 'rewrite' at level 4
              (assoc "action" attrs) action))  ;; 'rewrite' at level 3
      (push (list "action" newaction) attrs -1))   ;; no pair. Add a pair
    (setf (form-tag 2 1) attrs                     ;; 'rewrite' at level 2
          (form-list 1) form-tag                   ;; 'rewrite' at level 1
          (assoc current-formname DS) form-list))) ;; 'rewrite' at level 0

Can this code be optimized or condensed? Insights from senior

newlispers will go a long ways to further educate me.

Thanks

tim
Programmer since 1987. Unix environment.

hilti

#1
Hi Tim,



I would probably try to simplify the code which POSTS the form. Normally You won't need a nested structure for posted data from HTML forms.

Download Dragonfly and take a look at the request.lsp



Here's what it works like: http://www.rundragonfly.com/dragonfly_getpost">//http://www.rundragonfly.com/dragonfly_getpost



Cheers

Marc
--()o Dragonfly web framework for newLISP

http://dragonfly.apptruck.de\">http://dragonfly.apptruck.de

cormullion

#2
How about:


(set-ref  '("action" ?) l '("action" "http://localhost/cgi-bin/test.lsp") match)

Tim Johnson

#3
Quote from: "hilti"Hi Tim,



I would probably try to simplify the code which POSTS the form. Normally You won't need a nested structure for posted data from HTML forms.

Download Dragonfly and take a look at the request.lsp



Here's what it works like: http://www.rundragonfly.com/dragonfly_getpost">//http://www.rundragonfly.com/dragonfly_getpost



Cheers

Marc

Hi Marc, this has nothing to do with posting. It's about creating a form with data loaded

into it from a database.

thanks

tim
Programmer since 1987. Unix environment.

Tim Johnson

#4
Quote from: "cormullion"How about:


(set-ref  '("action" ?) l '("action" "http://localhost/cgi-bin/test.lsp") match)

Gotta try this. I will get back to you folks on it.

Thanks very much.

tim
Programmer since 1987. Unix environment.

Tim Johnson

#5
cormullion does it again. Here were have cut the LOC in half:

(unless(set-ref '("action" ?) attrs (list "action" newaction) match)
  (push (list "action" newaction) attrs -1))

But this raises a couple of more questions. Obviously the functor argument
match does its job here, but I'm trying to use 'match as standalone

and not getting the results I want:
> (set 'attrs '(("ID" "form0") ("method" "POST") ("name" "form0")))
(("ID" "form0") ("method" "POST") ("name" "form0"))
> (match '("ID" ?) attrs)
nil

What am I doing wrong?

thanks

tim
Programmer since 1987. Unix environment.

cormullion

#6
I don't think you're reflecting the pattern of the target list precisely enough.  This might work:


(match '(("ID" ?) *) attrs true)

Tim Johnson

#7
Quote from: "cormullion"I don't think you're reflecting the pattern of the target list precisely enough.  This might work:


(match '(("ID" ?) *) attrs true)

That's correct. Thanks.
Programmer since 1987. Unix environment.