Page 1 of 1

Call a function with &optional params from one with &rest

Posted: Tue May 06, 2014 8:31 am
by joeish80829
I have 2 functions FOO and BAR below. I would like to be able to call FOO from BAR a certain way.
BAR must have &rest args parameters. If I run BAR with only 2 arguments e.g.:

Code: Select all

(bar 1 2)
I would like that FOO be run with the 1 and 2 values put in by the call to BAR but also use the

Code: Select all

&optional (type 1) (mask 2)
parameters. So the result of running

Code: Select all

 (bar 1 2)
, would be FOO getting called like this

Code: Select all

 (foo 1 2 3 4)
. How would I do this??

Code: Select all

(defun foo (src1 src2 &optional (type 3) (mask 4))
  (test src1 src2 norm-type mask))

(defun bar (&rest args)
     (foo (first args)  (second args)  (third args)  (fourth args)))


I figured out what you were saying and , thanks..that worked perfectly. As far as code quality, would you say this is good coding. Seems a bit much and I was wondering if there was a way to trim it down

Code: Select all

;;double norm(InputArray src1, int normType=NORM_L2, InputArray mask=noArray())
;;double cv_norm(Mat* src1, int normType, Mat* mask) 
(defcfun ("cv_norm" %norm3) :void 
  (src1 mat)
  (norm-type :int)
  (mask mat))

;;double norm(InputArray src1, InputArray src2, int normType=NORM_L2, InputArray mask=noArray())
;;double cv_norm4(Mat* src1, Mat* src2, int normType, Mat* mask)
(defcfun ("cv_norm4" %norm4) :void 
  (src1 mat)
  (src2 mat)
  (norm-type :int)
  (mask mat))

(defun %%norm3 (src1 &optional (norm-type +norm-l2+) (mask (mat)))
  (%norm3 src1 norm-type mask))

(defun %%norm4 (src1 src2 &optional (norm-type +norm-l2+) (mask (mat)))
  (%norm4 src1 src2 norm-type mask))

(defun norm (&rest args)
  (if (eq (type-of (second args)) 'cv-mat)
      (apply #'%%norm4 args)
      (apply #'%%norm3 args)))

Re: Call a function with &optional params from one with &res

Posted: Sat May 10, 2014 2:09 pm
by Goheeca
I would just go with this:

Code: Select all

(defun bar (&rest args)
  (apply #'foo args))

Re: Call a function with &optional params from one with &res

Posted: Sun May 11, 2014 1:29 am
by joeish80829
Can u check out my latest edit:)

Re: Call a function with &optional params from one with &res

Posted: Sun May 11, 2014 5:41 am
by pjstirling
You should be using TYPEP rather than (EQ (TYPE-OF ...) ...)

Re: Call a function with &optional params from one with &res

Posted: Sun May 11, 2014 5:49 am
by joeish80829
Thank you for that...I noticed the time it takes to run is same but it its more concise and easier to type. Otherwise is the way I did my functions perfect in this case