* Re: list building and argz_smob
@ 2004-02-09 18:07 Brian S McQueen
2004-02-09 18:41 ` Paul Jarc
0 siblings, 1 reply; 4+ messages in thread
From: Brian S McQueen @ 2004-02-09 18:07 UTC (permalink / raw)
I wanted to update folks on the results of applying the advice you all
supplied a few weeks back, when I asked about list buildingand about an
argz_smob.
List Building:
I was building an output-list adding data as needed. cons* was one of the
suggestions of the folks on this list. Well after implementing this cons*
approach, and gaining some more insight into the functional approach to
programming, I found there was no need for incremental assembling of a
global list. In fact in the end I didn't even need the cons*. The
program is much better now. I eliminated the global list and eliminated
the clumsy list assembly process. The final result is like this:
(nop-printer "full_form"
(list
(string-append "pref_login=\"" pref-login "\"")
(if cgi-http-cookie
(let ((nop-item (cgi:cookie "nop_item")))
(if nop-item
(string-append "nop_item=\"" (frobnicate nop-item)
"\"")
"no_nop_item" ))
"no_nop_item")
(check-old-data pref-login)
(selected-groups pref-login)
(get-data pref-login)
(all-groups)
"devel_name=\"Brian McQueen\""
"devel_email=\"bqueen@nas.nasa.gov\"" ))
Argz:
This approach obviously produces a SCM list and hands it to a printer.
This is where I had used the argz_smob before. I had a few db libs around
which used argzs. After some thinking I found an argz can be treated
exactly like a SCM string, so I eliminated the argz_smob. Though thats
not all. I was going through some trouble to join the above SCM list into
a single massive SCM string, creating one massive argz, then the SCM
string containing the argz was passed to the C printer which handled it as
a single argz. This was a very clumsy and annoying approach, and as you
can see from the above sample, I found a better way to handle the SCM
list.
Turning the list into string was really bad, so I pondered this for a
while and noticed I could use the scm list handlers via calls from the C
world. So I decided to pass the SCM list directly to the C world which
then handles the SCM list, in fact by calling scm_map. So finally the C
is very simple and it does itz argz handling in a one liner of C code!
It is very simple:
scm_map(
scm_c_define_gsubr("anon-printer", 1, 0, 0, private_printer),
argz_list_scm, SCM_EOL);
This one liner goes over each argz in the incoming list and prints it.
Brian
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: list building and argz_smob
2004-02-09 18:07 list building and argz_smob Brian S McQueen
@ 2004-02-09 18:41 ` Paul Jarc
2004-02-09 18:51 ` Paul Jarc
2004-02-09 19:49 ` Thien-Thi Nguyen
0 siblings, 2 replies; 4+ messages in thread
From: Paul Jarc @ 2004-02-09 18:41 UTC (permalink / raw)
Cc: guile-user
Brian S McQueen <bqueen@nas.nasa.gov> wrote:
> (if cgi-http-cookie
> (let ((nop-item (cgi:cookie "nop_item")))
> (if nop-item
> (string-append "nop_item=\"" (frobnicate nop-item) "\"")
> "no_nop_item" ))
> "no_nop_item")
You might also like:
(let ((nop-item (and cgi-http-cookie (cgi:cookie "nop_item"))))
(if nop-item
(string-append "nop_item=\"" (frobnicate nop-item) "\"")
"no_nop_item"))
> scm_map(
> scm_c_define_gsubr("anon-printer", 1, 0, 0, private_printer),
> argz_list_scm, SCM_EOL);
I don't think you want to use scm_c_define_gsubr here. This should do
it:
scm_map(private_printer, argz_list_scm, SCM_EOL);
scm_c_define_gsubr is needed only once, to create the Scheme-visible
binding to the C function, and you might prefer using the SCM_DEFINE
snarfing macro instead of explicitly calling scm_c_Define_gsubr.
paul
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: list building and argz_smob
2004-02-09 18:41 ` Paul Jarc
@ 2004-02-09 18:51 ` Paul Jarc
2004-02-09 19:49 ` Thien-Thi Nguyen
1 sibling, 0 replies; 4+ messages in thread
From: Paul Jarc @ 2004-02-09 18:51 UTC (permalink / raw)
Cc: guile-user
I wrote:
> This should do it:
> scm_map(private_printer, argz_list_scm, SCM_EOL);
Well, no, that's not quite right, since scm_map expects a SCM value,
and private_printer is a C function. But you still don't want to use
scm_c_define_gsubr here, if this code will run more than once. Your
one-time init code can establish the binding, look up the SCM value,
and store that in a global variable, protecting it with
scm_gc_protect_object, and then pass the SCM value in that global
variable to scm_map.
paul
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: list building and argz_smob
2004-02-09 18:41 ` Paul Jarc
2004-02-09 18:51 ` Paul Jarc
@ 2004-02-09 19:49 ` Thien-Thi Nguyen
1 sibling, 0 replies; 4+ messages in thread
From: Thien-Thi Nguyen @ 2004-02-09 19:49 UTC (permalink / raw)
Cc: guile-user
From: prj@po.cwru.edu (Paul Jarc)
Date: Mon, 09 Feb 2004 13:41:05 -0500
(let ((nop-item (and cgi-http-cookie (cgi:cookie "nop_item"))))
(if nop-item
(string-append "nop_item=\"" (frobnicate nop-item) "\"")
"no_nop_item"))
alternatively:
(or (and=> (and cgi-http-cookie (cgi:cookie "nop_item"))
(lambda (nop-item)
(string-append ...)))
"no_nop_item")
this is similar to: (cond (EXP => (lambda (VAL) ...)) (else DEFAULT))
but slightly less verbose. i think the code is close to beautiful now;
finishing touch would be to zonk `string-append' somehow.
thi
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2004-02-09 19:49 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-02-09 18:07 list building and argz_smob Brian S McQueen
2004-02-09 18:41 ` Paul Jarc
2004-02-09 18:51 ` Paul Jarc
2004-02-09 19:49 ` Thien-Thi Nguyen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).