Optimizing 'deep' setf

Featuring the Dragonfly web framework
Locked
Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Optimizing 'deep' setf

Post by 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

Code: Select all

("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

Code: Select all

("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

Code: Select all

(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
Posts: 140
Joined: Sun Apr 19, 2009 10:09 pm
Location: Hannover, Germany
Contact:

Re: Optimizing 'deep' setf

Post by 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

Cheers
Marc
--()o Dragonfly web framework for newLISP
http://dragonfly.apptruck.de

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Optimizing 'deep' setf

Post by cormullion »

How about:

Code: Select all

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

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Optimizing 'deep' setf

Post by Tim Johnson »

hilti wrote: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

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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Optimizing 'deep' setf

Post by Tim Johnson »

cormullion wrote:How about:

Code: Select all

(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
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Optimizing 'deep' setf

Post by Tim Johnson »

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

Code: Select all

(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

Code: Select all

 match 
does its job here, but I'm trying to use 'match as standalone
and not getting the results I want:

Code: Select all

> (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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Re: Optimizing 'deep' setf

Post by cormullion »

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

Code: Select all

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

Tim Johnson
Posts: 253
Joined: Thu Oct 07, 2004 7:21 pm
Location: Palmer Alaska USA

Re: Optimizing 'deep' setf

Post by Tim Johnson »

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

Code: Select all

(match '(("ID" ?) *) attrs true)
That's correct. Thanks.
Programmer since 1987. Unix environment.

Locked