Let's say I have a vector ["a" "b" "c" "a" "a" "b"]
. If given a sequence ["a" "b"]
, how can I remove all instances of that sequence (in order)? Here, the result would just be ["c" "a"]
.
If sequences that need to be removed are known in advance, core.match
may be useful for your task:
(require '[clojure.core.match :refer [match]])
(defn remove-patterns [seq]
(match seq
["a" "b" & xs] (remove-patterns xs)
[x & xs] (cons x (remove-patterns xs))
[] ()))
(remove-patterns ["a" "b" "c" "a" "a" "b"]) ;; => ("c" "a")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With