multiple value defun

From: Geoffrey Summerhayes (sumrnot_at_NhOoStPmAaMil.com)
Date: 03/29/05

  • Next message: Kalle Olavi Niemitalo: "Re: Types"
    Date: Tue, 29 Mar 2005 00:48:38 -0500
    
    

    I was thinking about something I wanted to do with the painter code
    today and realized it would be nice if I could use multiple values
    without having to it show in the top level coding. Then
    looking at values and standard lambda lists as orthogonal as in
    (fn (values 3 4)(values 2 7)) == (fn 3 2)
    When I'd like to capture the 4 and 7 sometimes.
    Kind of dashed it off, sorry about the variable names.
    Looking for comments, replacements, improvements, etc.

    (defun parse-lambda-list (list)
      (let (binders mv-lists quick args)
        (do ((x list (rest x)))
            ((null x) (values (nreverse binders) (nreverse mv-lists)
                              (nreverse quick) (nreverse args)))
          (if (eq '&mv (first x))
              (let ((g (gensym)))
                (unless (listp (second x))
                  (error "Error argument for &mv not a list: ~S in ~S"
                         (second x) list))
                (push (second x) binders)
                (push g mv-lists)
                (push t quick)
                (push g args)
                (setf x (rest x))) ; skip what follows &mv
            (progn (push nil quick)
              (push (first x) args))))))

    (defun generate-funcall (fname arg-list quick)
      (cons fname
            (mapcar (lambda (arg use)
                      (if use
                          (list 'multiple-value-list arg)
                        arg))
                    arg-list quick)))

    (defmacro defun+ (fn-name arg-list &body body)
      (multiple-value-bind (b m q a) (parse-lambda-list arg-list)
        (if (null m)
            `(defun ,fn-name ,arg-list ,@body)
          (let ((fn (gensym)))
            `(progn
               (defun ,fn ,a (destructuring-bind ,b ,(cons 'list m)
                               ,@body))
               (defmacro ,fn-name (&rest args)
                 (generate-funcall ',fn args ',q)))))))

    Example:

    CL-USER 39 > (defun+ foo (a b &mv (c &optional d &rest r) &mv (e f))
                                   (list a b c d e f r))
    FOO

    CL-USER 40 > (foo 1 2 3 (values 4 5))
    (1 2 3 NIL 4 5 NIL)

    CL-USER 41 > (foo 1 2 (values 3 4 5 6 7) (values 4 5))
    (1 2 3 4 4 5 (5 6 7))

    --
    Geoff 
    

  • Next message: Kalle Olavi Niemitalo: "Re: Types"

    Relevant Pages

    • [Full-Disclosure] Re: Full-Disclosure digest, Vol 1 #1933 - 20 msgs
      ... read from multiple peoples articles that it isn't this kind of bug. ... > that about how bad Windows sucks from people who don't know enough about how ... I don't do it out in the public lists like ... >>because you have a bug up your bum about it and work to prove that stance. ...
      (Full-Disclosure)
    • Re: Difficulty with List Selection and New Records
      ... but one Patent can be assigned to multiple ... Doug Steele, Microsoft Access MVP ... I need to create a list that allows multiple selections. ... successfully created lists that allow multiple selections. ...
      (microsoft.public.access.gettingstarted)
    • Re: MOSS 2007 Out of the Box Workflows Broken
      ... I will add that choosing the Allow Multiple Seletions also prevents users ... from editing lists in datasheet view, exporting lists to excel, and using ... that field in custom workflows through SharePoint Designer. ... theworkflowuses the default Task content type to manageworkflowtasks we ...
      (microsoft.public.sharepoint.portalserver.development)
    • Re: Trouble w/ Value List
      ... What I am wanting is three drop down lists, two of which allow for multiple selections. ... Under some circumstances you may want to use a validation rule instead - you couldn't have a list of all the email addresses in the world but you could devise a validation rule expression which returned True if the text entered contained an "@" character. ... there are dangers - it's considered bad design to have multiple values in one table field (violates the "normalisation" rule of "1st Normal Form" to be specific). ...
      (microsoft.public.access.gettingstarted)
    • Re: Multiple criteria with multiple results in one cell
      ... multiple criteria. ... firstcell is the first cell of the output array, ... companies in a separate column on that worksheet based on a pay rate table ... drop-down lists on her site, but in those cases, it requires either having a ...
      (microsoft.public.excel.worksheet.functions)