In that example it would get rid of any vector elements that equaled "whatever".
I'm looking to erase certain elements if anywhere in that element it has a certain combination of letters. In my case, I want to erase any URLs that make it into the vector, so I would erase any element that contained "http" or "www" in it.
I tried googling this, but I have no clue what to look for. Can anyone offer their expertise? Is there a one line solution using vectorName.erase()?
Name:
Anonymous2009-11-28 7:59
>>13
Here's a more generic solution in CL:
(defun remove-badwords (sequence badwords)
(remove-if #'(lambda (x)
(dolist (s badwords)
(when (search s x :test #'equalp)
(return t))))
sequence))
CL-USER> (remove-badwords #("abcdef" "http://dis.4chan.org/prog/" "..A..." "huh") '("http" "www" "..."))
#("abcdef" "huh")
Another solution if the badwords are hardcoded, is to replace that DOLIST with (or (search ...) (search ...) (search ...), or just write a macro which does that for you:
;;; Which is exactly the desired expansion, but it uses EVAL at macroexpansion time, which might be considered a bit distateful. I also wrote a solution which involves nested backquotes, but it's a bit hard to read compared to the EVAL one, so I'm not including it here.
;;; REMOVE-BADWORDS using hardcoded badwords:
(defun remove-badwords (sequence)
(remove-if #'(lambda (x)
(do-log or ("http" "www" "...") s
`(search ,s x :test #'equalp)))
sequence))
;;; These were simple, and fun exercises, but in a real application, one might want to just use a regular expression:
(require :cl-ppcre) ; or (asdf:oos 'asdf:load-op '#:cl-ppcre)
(defun remove-badwords (sequence badwords)
(remove-if #'(lambda (x) (cl-ppcre:scan "http|www" x)) sequence))