Search code examples
clojuresequencesequencesseq

Clojure: how to test if a seq is a "subseq" of another seq


Is there an easy / idiomatic way in Clojure to test whether a given sequence is included within another sequence? Something like:

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

(where subseq? is a theoretical function that would do what I'm describing)

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


Solution

  • (defn subseq? [a b]
      (some #{a} (partition (count a) 1 b)))