Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove sequential matches in vector in Clojure?

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"].

like image 391
Andrew Avatar asked Apr 01 '16 15:04

Andrew


1 Answers

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")
like image 94
OlegTheCat Avatar answered Sep 19 '22 18:09

OlegTheCat