Function: PARTITION

Documentation

Split LIST into sub lists according to LAMBDAS. Each element of LIST will be passed to each element of LAMBDAS, the first function in LAMBDAS which returns T will cause that element to be collected into the corresponding list. Examples: (partition '(1 2 3) #'oddp #'evenp) => ((1 3) (2)) (partition '(1 2 3) #'oddp t) => ((1 3) (1 2 3)) (partition '(1 2 3) #'oddp #'stringp) => ((1 3) nil)

Source

(defun partition (list &rest lambdas)
  "Split LIST into sub lists according to LAMBDAS.

Each element of LIST will be passed to each element of LAMBDAS,
the first function in LAMBDAS which returns T will cause that
element to be collected into the corresponding list.

Examples:

 (partition '(1 2 3) #'oddp #'evenp) => ((1 3) (2))

 (partition '(1 2 3) #'oddp t) => ((1 3) (1 2 3))

 (partition '(1 2 3) #'oddp #'stringp) => ((1 3) nil)"
  (let ((collectors (mapcar (lambda (predicate)
                              (cons (case predicate
                                      ((t :otherwise) 
                                       (constantly t))
                                      ((nil)
                                       (constantly nil))
                                      (t predicate))
                                    (make-collector)))
                            lambdas)))
    (dolist (item list)
      (dolist* ((test-func . collector-func) collectors)
        (when (funcall test-func item)
          (funcall collector-func item))))
    (mapcar #'funcall (mapcar #'cdr collectors))))
Source Context