Function: -difference

-difference is a byte-compiled function defined in dash.el.

Signature

(-difference LIST1 LIST2)

Documentation

Return a new list with the distinct members of LIST1 that are not in LIST2.

The test for equality is done with equal, or with -compare-fn if that is non-nil.

View in manual

Source Code

;; Defined in ~/.emacs.d/elpa/dash-20260221.1346/dash.el
(defun -difference (list1 list2)
  "Return a new list with the distinct members of LIST1 that are not in LIST2.

The test for equality is done with `equal', or with `-compare-fn'
if that is non-nil."
  (declare (important-return-value t))
  (let (test len1 len2)
    (cond ((null list1) ())
          ((null list2) (-distinct list1))
          ;; Use a hash table if `-compare-fn' is a known hash table
          ;; test function and the subtrahend is long enough.
          ((and (setq test (dash--hash-test-fn))
                (setq len1 (length list1))
                (setq len2 (length list2))
                (> (max len1 len2) dash--short-list-length))
           (let ((ht1 (make-hash-table :test test :size len1))
                 (ht2 (make-hash-table :test test :size len2)))
             (--each list2 (puthash it t ht2))
             ;; Avoid duplicates by tracking visited items in `ht1'.
             (--filter (unless (or (gethash it ht2) (gethash it ht1))
                         (puthash it t ht1))
                       list1)))
          ((let ((member (dash--member-fn)) difference)
             (--each list1
               (unless (or (funcall member it list2)
                           (funcall member it difference))
                 (push it difference)))
             (nreverse difference))))))