What is idiomatic Clojure to “remove” a single instance from many in a list?

前端 未结 3 1180
悲&欢浪女
悲&欢浪女 2020-12-18 20:10

I have a list, which may contain elements that will compare as equal. I would like a similar list, but with one element removed. So from (:a :b :c :b :d) I would like to

3条回答
  •  梦毁少年i
    2020-12-18 21:05

    I usually solve these problems with a higher-order function like split-with, but someone's already done that. Sometimes it's more readable or more efficient to work at a more primitive level, so here's a better version of your original looping code, using lazy sequences and generalized to take a predicate for removal instead of being constrained to equality checks:

    (defn remove-once [pred coll]
      ((fn inner [coll]
         (lazy-seq
          (when-let [[x & xs] (seq coll)]
            (if (pred x)
              xs
              (cons x (inner xs))))))
       coll))
    
    
    user> (remove-once #{:b} [:a :b :c :b :d])
    (:a :c :b :d)
    

提交回复
热议问题