Dynamic function creation

Pondering the philosophy behind the language
Locked
itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Dynamic function creation

Post by itistoday »

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

Code: Select all

(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:

Code: Select all

(define (log[var] a b c)
	(string var ": " a b c)
)
Then you would call it like this:

Code: Select all

(logerr "jazziffy" mylist)
And get the output:
err: jazzify (1 2 3) nil
You might even add some conditional syntactic sugar to it like so:

Code: Select all

(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
Posts: 388
Joined: Thu May 08, 2008 1:24 am
Location: Croatia
Contact:

Post by 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.

itistoday
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Post by itistoday »

Kazimir Majorinc wrote: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
Posts: 429
Joined: Sun Dec 02, 2007 5:10 pm
Contact:

Post by itistoday »

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.

Locked