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

Discussion of Common Lisp

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

Postby joeish80829 » Tue May 06, 2014 8:31 am

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)))
Last edited by joeish80829 on Sun May 11, 2014 1:28 am, edited 2 times in total.
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

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

Postby Goheeca » Sat May 10, 2014 2:09 pm

I would just go with this:
Code: Select all
(defun bar (&rest args)
  (apply #'foo args))
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version).
User avatar
Goheeca
 
Posts: 213
Joined: Thu May 10, 2012 12:54 pm

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

Postby joeish80829 » Sun May 11, 2014 1:29 am

Can u check out my latest edit:)
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

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

Postby pjstirling » Sun May 11, 2014 5:41 am

You should be using TYPEP rather than (EQ (TYPE-OF ...) ...)
pjstirling
 
Posts: 79
Joined: Sun Nov 28, 2010 4:21 pm

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

Postby joeish80829 » Sun May 11, 2014 5:49 am

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
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am


Return to Common Lisp

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot] and 2 guests

cron