* Programmatically creating functions
@ 2008-10-21 17:26 Ian Eure
0 siblings, 0 replies; 6+ messages in thread
From: Ian Eure @ 2008-10-21 17:26 UTC (permalink / raw
To: help-gnu-emacs
So, I have a list of symbols:
'(foo bar baz)
I want to iterate over the list and create a function from each which
does something like this:
(defun call-foo ()
(interactive)
(invoke-stuff 'foo)
How can I accomplish this? I can't figure out how to create the
function. I've tried a number of approaches, but have not met with
success.
- eval'ing the defun. Returns a function symbol, but I can't call
it. Maybe it's only created within the scope of the (eval) and not
callable from outside?
- Creating a symbol and using fset to assign a lambda to it's
function cell. It sort of works, but I'm unclear on how to pass a
variable function name to defun, nor am I clear on how I can make sure
it calls invoke-stuff with the right symbol.
Help?
- Ian
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Programmatically creating functions
[not found] <mailman.1730.1224610023.25473.help-gnu-emacs@gnu.org>
@ 2008-10-21 18:22 ` aartist
2008-10-21 18:34 ` Joost Diepenmaat
1 sibling, 0 replies; 6+ messages in thread
From: aartist @ 2008-10-21 18:22 UTC (permalink / raw
To: help-gnu-emacs
Do you think that skeleton or macro can help?
On Oct 21, 1:26 pm, Ian Eure <i...@digg.com> wrote:
> So, I have a list of symbols:
>
> '(foo bar baz)
>
> I want to iterate over the list and create a function from each which
> does something like this:
>
> (defun call-foo ()
> (interactive)
> (invoke-stuff 'foo)
>
> How can I accomplish this? I can't figure out how to create the
> function. I've tried a number of approaches, but have not met with
> success.
>
> - eval'ing the defun. Returns a function symbol, but I can't call
> it. Maybe it's only created within the scope of the (eval) and not
> callable from outside?
>
> - Creating a symbol and using fset to assign a lambda to it's
> function cell. It sort of works, but I'm unclear on how to pass a
> variable function name to defun, nor am I clear on how I can make sure
> it calls invoke-stuff with the right symbol.
>
> Help?
>
> - Ian
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Programmatically creating functions
[not found] <mailman.1730.1224610023.25473.help-gnu-emacs@gnu.org>
2008-10-21 18:22 ` Programmatically creating functions aartist
@ 2008-10-21 18:34 ` Joost Diepenmaat
2008-10-21 18:57 ` Andreas Politz
1 sibling, 1 reply; 6+ messages in thread
From: Joost Diepenmaat @ 2008-10-21 18:34 UTC (permalink / raw
To: help-gnu-emacs
Ian Eure <ian@digg.com> writes:
> So, I have a list of symbols:
>
> '(foo bar baz)
>
> I want to iterate over the list and create a function from each which
> does something like this:
>
> (defun call-foo ()
> (interactive)
> (invoke-stuff 'foo)
>
> How can I accomplish this? I can't figure out how to create the
> function. I've tried a number of approaches, but have not met with
> success.
>
> - eval'ing the defun. Returns a function symbol, but I can't call
> it. Maybe it's only created within the scope of the (eval) and not
> callable from outside?
>
> - Creating a symbol and using fset to assign a lambda to it's
> function cell. It sort of works, but I'm unclear on how to pass a
> variable function name to defun, nor am I clear on how I can make sure
> it calls invoke-stuff with the right symbol.
I'm not /quite/ sure where you've got problems, but in this case elisp's
lack of closures hurts. IMHO the simplest way to get what you want is to
use a macro:
(defmacro make-caller-macro (symbol)
`(defun ,(intern (concat "call-" (symbol-name symbol))) ()
(,symbol)))
But that won't evaluate the argument, so you'd more or less have to use
eval as well:
(dolist (s '(foo bar)) (eval `(make-caller-macro ,s)))
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Programmatically creating functions
2008-10-21 18:34 ` Joost Diepenmaat
@ 2008-10-21 18:57 ` Andreas Politz
2008-10-25 18:28 ` Ian Eure
[not found] ` <mailman.2073.1224959342.25473.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 6+ messages in thread
From: Andreas Politz @ 2008-10-21 18:57 UTC (permalink / raw
To: help-gnu-emacs
Joost Diepenmaat wrote:
> Ian Eure <ian@digg.com> writes:
>
>> So, I have a list of symbols:
>>
>> '(foo bar baz)
>>
>> I want to iterate over the list and create a function from each which
>> does something like this:
>>
>> (defun call-foo ()
>> (interactive)
>> (invoke-stuff 'foo)
>>
>> How can I accomplish this? I can't figure out how to create the
>> function. I've tried a number of approaches, but have not met with
>> success.
>>
>> - eval'ing the defun. Returns a function symbol, but I can't call
>> it. Maybe it's only created within the scope of the (eval) and not
>> callable from outside?
>>
>> - Creating a symbol and using fset to assign a lambda to it's
>> function cell. It sort of works, but I'm unclear on how to pass a
>> variable function name to defun, nor am I clear on how I can make sure
>> it calls invoke-stuff with the right symbol.
>
> I'm not /quite/ sure where you've got problems, but in this case elisp's
> lack of closures hurts. IMHO the simplest way to get what you want is to
> use a macro:
>
> (defmacro make-caller-macro (symbol)
> `(defun ,(intern (concat "call-" (symbol-name symbol))) ()
> (,symbol)))
>
> But that won't evaluate the argument, so you'd more or less have to use
> eval as well:
>
> (dolist (s '(foo bar)) (eval `(make-caller-macro ,s)))
>
Does this work ? (in general)
(defun make-fun (name)
(fset (intern (format "call-%s" name))
`(lambda nil
,(format "call-%s is a very good function." name)
(interactive)
(message "I was once %s" (quote ,name)))))
(mapcar 'make-fun '(foo bar baz))
(describe-function 'call-baz)
(call-bar)
-ap
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Programmatically creating functions
2008-10-21 18:57 ` Andreas Politz
@ 2008-10-25 18:28 ` Ian Eure
[not found] ` <mailman.2073.1224959342.25473.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 6+ messages in thread
From: Ian Eure @ 2008-10-25 18:28 UTC (permalink / raw
To: Andreas Politz; +Cc: help-gnu-emacs
On Oct 21, 2008, at 11:57 AM, Andreas Politz wrote:
> Joost Diepenmaat wrote:
>> Ian Eure <ian@digg.com> writes:
>>> So, I have a list of symbols:
>>>
>>> '(foo bar baz)
>>>
>>> I want to iterate over the list and create a function from each
>>> which
>>> does something like this:
>>>
>>> (defun call-foo ()
>>> (interactive)
>>> (invoke-stuff 'foo)
>>>
>>> How can I accomplish this? I can't figure out how to create the
>>> function. I've tried a number of approaches, but have not met with
>>> success.
>>>
>>> - eval'ing the defun. Returns a function symbol, but I can't call
>>> it. Maybe it's only created within the scope of the (eval) and not
>>> callable from outside?
>>>
>>> - Creating a symbol and using fset to assign a lambda to it's
>>> function cell. It sort of works, but I'm unclear on how to pass a
>>> variable function name to defun, nor am I clear on how I can make
>>> sure
>>> it calls invoke-stuff with the right symbol.
>> I'm not /quite/ sure where you've got problems, but in this case
>> elisp's
>> lack of closures hurts. IMHO the simplest way to get what you want
>> is to
>> use a macro:
>> (defmacro make-caller-macro (symbol) `(defun ,(intern (concat
>> "call-" (symbol-name symbol))) () (,symbol)))
>> But that won't evaluate the argument, so you'd more or less have to
>> use
>> eval as well:
>> (dolist (s '(foo bar)) (eval `(make-caller-macro ,s)))
>
>
> Does this work ? (in general)
>
Yes, with a little hacking, it works:
(defvar sql-connection-alist
'((poola
(sql-product 'mysql)
(sql-server "pool-a")
(sql-user "me")
(sql-password "mypass")
(sql-database "default")
(sql-port 3306))
(poolb
(sql-product 'mysql)
(sql-server "pool-b")
(sql-user "me")
(sql-password "mypass")
(sql-database "default")
(sql-port 3307)))
"AList of preset connections for `sql-connect-preset'.")
(defun sql-connect-preset (name)
"Connect to a predefined SQL connection listed in `sql-connection-
alist'"
(require 'sql)
(let ((conn (cdr (assoc name sql-connection-alist)))
(sql-name (symbol-name name)))
(eval `(let ,conn
(flet ((sql-get-login (&rest what)))
(sql-product-interactive sql-product))))))
(defun sql-convenience ()
(interactive)
(mapcar (lambda (conn)
(let ((name (car conn)))
(fset (intern (format "sql-%s" name))
`(lambda nil
,(format "Connect to %s SQL preset." name)
(interactive)
(sql-connect-preset ',name)))))
sql-connection-alist))
So now I can define preset connections and get sql-* methods to invoke
them directly.
Thank you for the help.
- Ian
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Programmatically creating functions
[not found] ` <mailman.2073.1224959342.25473.help-gnu-emacs@gnu.org>
@ 2008-10-28 20:29 ` Emil
0 siblings, 0 replies; 6+ messages in thread
From: Emil @ 2008-10-28 20:29 UTC (permalink / raw
To: help-gnu-emacs
On Oct 25, 11:28 am, Ian Eure <i...@digg.com> wrote:
> On Oct 21, 2008, at 11:57 AM, Andreas Politz wrote:
>
>
>
> > Joost Diepenmaat wrote:
> >> Ian Eure <i...@digg.com> writes:
> >>> So, I have a list of symbols:
>
> >>> '(foo bar baz)
>
> >>> I want to iterate over the list and create a function from each
> >>> which
> >>> does something like this:
>
> >>> (defun call-foo ()
> >>> (interactive)
> >>> (invoke-stuff 'foo)
>
> >>> How can I accomplish this? I can't figure out how to create the
> >>> function. I've tried a number of approaches, but have not met with
> >>> success.
>
> >>> - eval'ing the defun. Returns a function symbol, but I can't call
> >>> it. Maybe it's only created within the scope of the (eval) and not
> >>> callable from outside?
>
> >>> - Creating a symbol and using fset to assign a lambda to it's
> >>> function cell. It sort of works, but I'm unclear on how to pass a
> >>> variable function name to defun, nor am I clear on how I can make
> >>> sure
> >>> it calls invoke-stuff with the right symbol.
> >> I'm not /quite/ sure where you've got problems, but in this case
> >> elisp's
> >> lack of closures hurts. IMHO the simplest way to get what you want
> >> is to
> >> use a macro:
> >> (defmacro make-caller-macro (symbol) `(defun ,(intern (concat
> >> "call-" (symbol-name symbol))) () (,symbol)))
> >> But that won't evaluate the argument, so you'd more or less have to
> >> use
> >> eval as well:
> >> (dolist (s '(foo bar)) (eval `(make-caller-macro ,s)))
>
> > Does this work ? (in general)
>
> Yes, with a little hacking, it works:
>
> (defvar sql-connection-alist
> '((poola
> (sql-product 'mysql)
> (sql-server "pool-a")
> (sql-user "me")
> (sql-password "mypass")
> (sql-database "default")
> (sql-port 3306))
> (poolb
> (sql-product 'mysql)
> (sql-server "pool-b")
> (sql-user "me")
> (sql-password "mypass")
> (sql-database "default")
> (sql-port 3307)))
> "AList of preset connections for `sql-connect-preset'.")
>
> (defun sql-connect-preset (name)
> "Connect to a predefined SQL connection listed in `sql-connection-
> alist'"
> (require 'sql)
> (let ((conn (cdr (assoc name sql-connection-alist)))
> (sql-name (symbol-name name)))
> (eval `(let ,conn
> (flet ((sql-get-login (&rest what)))
> (sql-product-interactive sql-product))))))
>
> (defun sql-convenience ()
> (interactive)
> (mapcar (lambda (conn)
> (let ((name (car conn)))
> (fset (intern (format "sql-%s" name))
> `(lambda nil
> ,(format "Connect to %s SQL preset." name)
> (interactive)
> (sql-connect-preset ',name)))))
> sql-connection-alist))
>
> So now I can define preset connections and get sql-* methods to invoke
> them directly.
Here is another attempt I wrote recently :
(setq sql-connection-alist
'((local
(sql-product 'mysql)
(sql-server "<server>")
(sql-user "<user>")
(sql-password "<password>")
(sql-database "<database>")
(sql-port 3306))
(qa
(sql-product 'mysql)
(sql-server "<server>")
(sql-user "<user>")
(sql-password "<password>")
(sql-database "<database>")
(sql-port 3306))))
(defun sql-connect-preset (name)
"Connect to a predefined SQL connection listed in `sql-connection-
alist'"
(eval `(let ,(cdr (assoc name sql-connection-alist))
(flet ((sql-get-login (&rest what)))
(sql-product-interactive sql-product)))))
;; iterate over the alist and create functions
(mapcar '(lambda(x)
(let ((ds-name (symbol-name x)))
(eval (car (read-from-string
(format "(defun sql-%s() (interactive) (sql-connect-preset '%s))"
ds-name ds-name))))))
(mapcar 'car sql-connection-alist))
(provide 'mysql-utils)
Require mysql-utils and it i becomes available as M-x sql-local, or M-
x sql-qa.
emil
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2008-10-28 20:29 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <mailman.1730.1224610023.25473.help-gnu-emacs@gnu.org>
2008-10-21 18:22 ` Programmatically creating functions aartist
2008-10-21 18:34 ` Joost Diepenmaat
2008-10-21 18:57 ` Andreas Politz
2008-10-25 18:28 ` Ian Eure
[not found] ` <mailman.2073.1224959342.25473.help-gnu-emacs@gnu.org>
2008-10-28 20:29 ` Emil
2008-10-21 17:26 Ian Eure
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).