Dynamic function creation

Started by itistoday, November 22, 2008, 04:12:59 PM

Previous topic - Next topic

itistoday

Let me know if you've seen this sort of thing in another language.  Let's say you have the following functions:


(define (foo a b c)
(string "error: " a b c)
)
(define (bar a b c)
(string "info: " a b c)
)


Wouldn't it be cool if you could eliminate the need for having multiple function definitions in certain situations? You could do something like this:


(define (log[var] a b c)
(string var ": " a b c)
)


Then you would call it like this:


(logerr "jazziffy" mylist)

And get the output:


Quoteerr: jazzify (1 2 3) nil


 You might even add some conditional syntactic sugar to it like so:


(define (log[-?var] a b c)
(string var ": " a b c)
)

; call it
(log-err "jazzify) ; => err: jazzify nil nil
(log-info "jazzify") ; => info: jazzify nil nil
(log- "jazzify") ; => nil: jazzify nil nil
(log "jazzify") ; => nil: jazzify nil nil


This is admittedly a very very stupid example, but I'm sure this could turn out to be really useful in some situations where otherwise you'd have multiple functions essentially doing the same thing, they could be called "newLISP function templates", or something. :-)
Get your Objective newLISP groove on.

Kazimir Majorinc

#1
I see some advantage od (logerr a b c), but ((log err) a b c) and (log err a b c) seem to be very close.
http://kazimirmajorinc.com/\">WWW site; http://kazimirmajorinc.blogspot.com\">blog.

itistoday

#2
Quote from: "Kazimir Majorinc"I see some advantage od (logerr a b c), but ((log err) a b c) and (log err a b c) seem to be very close.


Like I said, this log function obviously can be written to work in other ways, but if I think of a good example that I can post here I will (I do have a situation currently where this would be helpful, but I can't post the code here).



Edit: a use of this technique would be check to see the value of 'var', this way the function could behave differently based on how it's named.
Get your Objective newLISP groove on.

itistoday

#3
On second thought, this probably wouldn't be a very good idea, as it could lead to ambiguity and unnecessary complexity. :-p
Get your Objective newLISP groove on.