[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best way to call a user defined hook (written in guile) from C when

From: Thien-Thi Nguyen
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
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

() address@hidden
() 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.  ;-)


reply via email to

[Prev in Thread] Current Thread [Next in Thread]