Multiple ways of doing things
Posted: Fri May 07, 2010 8:20 pm
For some things i noticed multiple ways of doing things:
Allowing the user to define 'a type of function' this might be some kind of macro that allows for things inside to be defined you have various approaches:
Via methods.Via hash table, or other type of dictionary.(Advantage of being special variable)And via renamed function which is a bad idea, because you might accidentally collide with it, a variant is to make a special package for these functions.
Higher order functions vs methods. Lets say you have some thing, and you want to do it in different ways. You could useAnd then provide different ways. Or you could pass a function, and pass a different function if you want to do it another way, you could pass this function via special variable. You could pass the way with special var *way*. Which is what the GIL project does to determine the language.
Methods have the nice quality that you can incrementally add onto them. But if you want to alter one of them for one use without changing existing methods, you need a new way, and an rather arbitrary name for it. The higher order function doesn't have this problem, but that is then not extensible. I guess the combination, used like this is the answer i am looking for:And there is the Functions vs objects thing, and the functions vs macros.Macros act like a LAMBDA-without-arguments like that sometimes. Sometimes you want to store something intermediately and 'run' it later, then the superiority from the user perspective of LAMBDA shows. And the question if and when you can trust LAMBDA to inline if you're not using this function. A LAMBDA without arguments can also be stored with a object storing the objects being used, combined with a function that emulates funcall.(you need garbage collection on the objects) Often handy, if you don't have a lib to store functions.
Functions via objects is a much deeper thing, though.
Not sure if this is worth posting. But it's too quiet
Anyway, know any other similar things that can be done in a different way? Would it be suitable to have stuff like this in the CLiki?
Allowing the user to define 'a type of function' this might be some kind of macro that allows for things inside to be defined you have various approaches:
Via methods.
Code: Select all
(defgeneric keyword-macro-gen (name form))
(defmacro def-keyword-macro (name args &body body)
(with-gensyms (namevar)
`(defmethod keyword-macro-gen ((,namevar (eql ,name)) (,args list))
,@body)))
(defmacro keyword-macro (name &rest form)
(keyword-macro-gen name form))Code: Select all
(defvar *keyword-macros* (make-hash-table))
(defmacro def-keyword-macro (name args &body body)
`(setf (gethash ',name *keyword-macros*) (lambda ,args ,@body)))
(defmacro keyword-macro (name &rest form)
(apply (gethash name *keyword-macros*) form))Higher order functions vs methods. Lets say you have some thing, and you want to do it in different ways. You could use
Code: Select all
(defgeneric do-something (way args))
(defvar *way*)
(defun do-something-* (args) (do-something *way* args))Methods have the nice quality that you can incrementally add onto them. But if you want to alter one of them for one use without changing existing methods, you need a new way, and an rather arbitrary name for it. The higher order function doesn't have this problem, but that is then not extensible. I guess the combination, used like this is the answer i am looking for:
Code: Select all
(lambda (args)
(if (condition args) (use-own-thing) (use-existing-methods)))Code: Select all
(defun do-something-before-fun (fun)
(do-something) (funcall fun))
(defmacro do-something-before (&body body) ;;These are very straight forward enough
`(do-something-before-fun (lambda () ,@body)))Functions via objects is a much deeper thing, though.
Not sure if this is worth posting. But it's too quiet