Name: Anonymous 2010-07-22 23:33
Racket
(eval-when (:compile-toplevel :load-toplevel :execute)
(defun symbolify (&rest symbols)
(intern (apply #'concatenate 'string (mapcar #'string symbols)))))
(defmacro with-gensyms (names &body body)
`(let ,(mapcar #'(lambda (name) `(,name (gensym ,(string name)))) names)
,@body))
(defmacro shorthand (name &key (key 'grab) (size 42) (type 'u32))
(with-gensyms (n)
(let ((var-name (symbolify '* name 's '*))
(fun-name (symbolify 'grab- name)))
`(progn
(defparameter ,var-name (make-array ,size :element-type ',type))
(defun ,fun-name (,n)
(,key (aref ,var-name ,n)))))))
(deftype u32 () '(unsigned-byte 32))
;;; define your own
(defun grab (n) (print n))
;;; Example:
(shorthand small-bomb :type t :size 10)
;;; expands to =>
(progn
(defparameter *small-bombs* (make-array 10 :element-type 't))
(defun grab-small-bomb (#:n1244) (grab (aref *small-bombs* #:n1244))))
;; Example usage
CL-USER> (fill *small-bombs* 'beatos)
#(BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS BEATOS)
CL-USER> (grab-small-bomb 3)
BEATOS BEATOS
; the first is printed, the second is the returned value