Clojure: how to check if seq is a "subsector" of another seq

Is there a simple / idiomatic way in Clojure to check if a given sequence is included in another sequence? Sort of:

(subseq? [4 5 6] (range 10))  ;=> true
(subseq? [4 6 5] (range 10))  ;=> false
(subseq? "hound" "greyhound") ;=> true

(where subseq?is the theoretical function that will do what I describe)

It seems that there is no such function in the kernel or other Clojure libraries ... provided that true, is there a relatively simple way to implement such a function?

+3
source share
2 answers
(defn subseq? [a b]
  (some #{a} (partition (count a) 1 b)))
+6
source
(defn subseq? [target source] 
  (pos? (java.util.Collections/indexOfSubList (seq source) (seq target))))
+2
source

All Articles