would like to enter multiple arguments w/o progn to defmacro

Discussion of Common Lisp

would like to enter multiple arguments w/o progn to defmacro

Postby joeish80829 » Sun Jul 06, 2014 8:45 pm

here is the macro, it is used to time 1 or more functions:

Code: Select all
  (defmacro $ (form &optional (count-form 1000000)) `(time (dotimes (_ ,count-form) ((lambda () ,form)))))

for 1 function I run like this:

Code: Select all
($ (function))


for multiple functions I run like this:

Code: Select all
($ (progn (function) (function)))


how can I make it so I can run multiple functions like this:

Code: Select all
($ (function) (function))


any help is appreciated:)

(it can be changed to a defun to make this happen but the count-form would need to default to 1,000,000 and it would need to stay optional):
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: would like to enter multiple arguments w/o progn to defm

Postby logxor » Sat Jul 12, 2014 10:57 am

Depends on how you want to specify the count-form in the multiple-argument version.
logxor
 
Posts: 17
Joined: Tue May 27, 2014 10:56 am
Location: Portland, OR

Re: would like to enter multiple arguments w/o progn to defm

Postby joeish80829 » Wed Jul 16, 2014 9:11 pm

Thank you very much for your reply:)...for my purposes I have to be able to run:


Code: Select all
 ($ (function) (function))


to time multiple functions at 1000,000 iterations

I have to be able to run:


Code: Select all
 ($ (function))


to time one function at 1000,000 iterations

I have to be able to run:

Code: Select all
 ($ (function) 1000)


to time one function at 1000 iterations

and I have to be able to run:


Code: Select all
 ($ (function) (function) 1000)


to time multiple functions at 1000 iterations
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: would like to enter multiple arguments w/o progn to defm

Postby logxor » Fri Jul 18, 2014 10:41 am

If it was me then I wouldn't bother with tricky argument-list-parsing techniques, but in this case, what you want isn't complex to implement. The macro just needs to take a &rest parameter and to pluck off the last element if it's an integer (indicating the count-form). If I wanted the count-form to be evaluated, as opposed to a self-evaluating integer invariably, then things would get a bit hairier than the code below:

Code: Select all
(defmacro $ (&rest arguments)
  (let ((last (last arguments))
        (count-form 1000000))
    (when (integerp (car last))
      (setq count-form (car last))
      ;; Exclude COUNT-FORM from the list.
      (setq arguments (ldiff arguments last)))
    `(time (dotimes (_ ,count-form) ,@arguments))))
logxor
 
Posts: 17
Joined: Tue May 27, 2014 10:56 am
Location: Portland, OR

Re: would like to enter multiple arguments w/o progn to defm

Postby joeish80829 » Fri Jul 18, 2014 4:45 pm

Thanks man, I really appreciate that, that will help my mind travel diiferent directions,now , knowing that:)

Take Care
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 11 guests