Advertisement
bahman

seq-intersect-p

Sep 8th, 2023 (edited)
2,280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.14 KB | Source Code | 0 0
  1. (defun seq-intersect-p (seq1 seq2 &rest sequences)
  2.   "Determine if the intersection of SEQ1, SEQ2 and SEQUENCES is non-nil."
  3.   (cl-do* ((sequences `(,seq1 ,seq2 ,@sequences) (cdr sequences))
  4.            (seq (car sequences) (car sequences))
  5.            (elements (make-hash-table :test #'equal))
  6.            (intersect-p nil))
  7.       ((or (seq-empty-p sequences)) intersect-p)
  8.     (cl-do* ((seq seq (cdr seq))
  9.              (element (car seq) (car seq)))
  10.         ((or intersect-p (seq-empty-p seq)) intersect-p)
  11.       (if (ht-get elements element)
  12.           (setf intersect-p t)
  13.         (ht-set elements element t)))))
  14.  
  15. (defun test-seq-intersect-p ()
  16.   "Test cases."
  17.   (cl-assert (null (seq-intersect-p '()
  18.                                     '())))
  19.   (cl-assert (null (seq-intersect-p '(1)
  20.                                     '())))
  21.   (cl-assert (null (seq-intersect-p '(1 2)
  22.                                     '(3 4)
  23.                                     '(5 6))))
  24.   (cl-assert (seq-intersect-p '(1 2)
  25.                               '(3 4)
  26.                               '(5 6)
  27.                               '(1)))
  28.   t)
  29.  
  30. (test-seq-intersect-p)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement