Advertisement
bahman

seq-intersect-p v2 - simplified

Sep 11th, 2023
1,864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.80 KB | Source Code | 0 0
  1. (require 'seq)
  2. (require 'cl-lib)
  3.  
  4. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  5.  
  6. (defun seq-intersect-p (first second &rest sequences)
  7.   "Determine if FIRST, SECOND and any of the sequences in SEQUENCES have
  8. an intersection."
  9.   (if (seq-empty-p sequences)
  10.       (seq-intersection first second)
  11.     (or (seq-intersection first second)
  12.         (apply #'seq-intersect-p
  13.                first
  14.                (seq-first sequences)
  15.                `,@(seq-rest sequences))
  16.         (apply #'seq-intersect-p
  17.                second
  18.                (seq-first sequences)
  19.                `,@(seq-rest sequences))
  20.         (apply #'seq-intersect-p
  21.                (seq-first sequences)
  22.                (seq-elt sequences 2)
  23.                `,@(seq-rest (seq-rest sequences))))))
  24.  
  25. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  26.  
  27. (progn
  28.  
  29.   ;;; lists
  30.  
  31.   (cl-assert (not (seq-intersect-p '()
  32.                                    '()))
  33.              t
  34.              "Empty lists do not intersect.")
  35.  
  36.   (cl-assert (not (seq-intersect-p '(1)
  37.                                    '()))
  38.              t
  39.              "Empty lists do not intersect w/ non-empty lists.")
  40.  
  41.   (cl-assert (not (seq-intersect-p '(1 2)
  42.                                    '(3 4)
  43.                                    '(5 6)))
  44.              t
  45.              "Lists w/ unique elements do not intersect.")
  46.  
  47.   (cl-assert (seq-intersect-p '(1 2)
  48.                               '(3 4)
  49.                               '(4))
  50.              t
  51.              "This must have an intersection!")
  52.  
  53.  
  54.   (cl-assert (not (seq-intersect-p '(1 1 2)
  55.                                    '(3 4)
  56.                                    '(5 6)))
  57.              t
  58.              "Duplicate elements contained to a list should not count towards intersection.")
  59.  
  60.   ;;; strings
  61.  
  62.   (cl-assert (not (seq-intersect-p ""
  63.                                    ""))
  64.              t
  65.              "Empty strings do not intersect.")
  66.  
  67.   (cl-assert (not (seq-intersect-p "a"
  68.                                    ""))
  69.              t
  70.              "Empty strings do not intersect w/ non-empty strings.")
  71.  
  72.   (cl-assert (not (seq-intersect-p "fep"
  73.                                    "bar"
  74.                                    "zug"))
  75.              t
  76.              "Strings w/ unique elements do not intersect.")
  77.  
  78.   (cl-assert (seq-intersect-p "fep"
  79.                               "bar"
  80.                               "zug"
  81.                               "z")
  82.              t
  83.              "This must have an intersection!")
  84.  
  85.  
  86.   (cl-assert (not (seq-intersect-p "foo"
  87.                                    "bar"
  88.                                    "zug"))
  89.              t
  90.              "Duplicate chars contained to a list should not count towards intersection."))
  91.  
Tags: emacs lisp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement