From: Thien-Thi Nguyen <ttn@gnuvola.org>
To: rixed@happyleptic.org
Cc: guile-user@gnu.org
Subject: Re: Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters
Date: Mon, 05 Jul 2010 11:57:51 +0200 [thread overview]
Message-ID: <87wrtantcw.fsf@ambire.localdomain> (raw)
In-Reply-To: <20100705085635.GB9492@apc> (rixed@happyleptic.org's message of "Mon, 5 Jul 2010 10:56:36 +0200")
() rixed@happyleptic.org
() Mon, 5 Jul 2010 10:56:36 +0200
Can anyone suggest a better way to do this ?
For "a set of named values", you can use an association list.
If the paren verbosity is off-putting, a common way is to take/show
plists externally (minimal (one) set of parens). So, user sees:
(k1 v1 k2 v2 ...)
which is not so threatening, but you manipulate internally:
((k1 . v1)
(k2 . v2)
...)
This requires a transform, but you do that anyway
(as part of validating the user input), right?
All this presumes that there is no need to specify all keys all the time.
(Re-reading your post, perhaps i am misunderstanding the problem question.)
If the problem really is: how to avoid unwieldy argument lists, the answer
is still "use an association list", but pass to the user a procedure that
encapsulates it. For example:
(define (query-proc alist)
"Encapsulate ALIST; return a procedure to query it."
(lambda (key)
(assoc-ref alist key)))
(define ALIST '((k1 . v1) (k2 . v2)))
(define QUERY (query-proc ALIST))
;; On the user side:
(define (monitor query)
(for-each (lambda (key)
(simple-format #t "k: ~S~%v: ~S~%" key (query key)))
'(k1 k2)))
This example is read-only; if you want ‘monitor’ to be able to munge
you can change ‘query-proc’ to perhaps ‘query/munge-proc’:
(define (query/munge-proc alist)
"Encapsulate ALIST; return a procedure query/munge it."
(lambda (key . newval)
(if (null? newval)
(assoc-ref alist key)
(set! alist (assoc-set! alist key (car newval))))))
(It all depends on how much you trust the users. ;-)
thi
next prev parent reply other threads:[~2010-07-05 9:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-07-05 8:56 Best way to call a user defined hook (written in guile) from C when the hook need plenty parameters rixed
2010-07-05 9:57 ` Thien-Thi Nguyen [this message]
2010-07-07 14:33 ` Cedric Cellier
2010-07-05 20:18 ` Neil Jerram
2010-07-06 11:22 ` Cedric Cellier
2010-07-08 19:51 ` Andy Wingo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87wrtantcw.fsf@ambire.localdomain \
--to=ttn@gnuvola.org \
--cc=guile-user@gnu.org \
--cc=rixed@happyleptic.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).