删除值包含一个或多个给定 collection 个单词的 collection 元素的最惯用方法是什么?
What's the most idiomatic way to to remove collection elements with values containing one or more of a given collection of words?
假设我想删除 banned-from-house collection:
中提及动物的列表元素
(def list (atom [{:animal "a quick happy brown fox that rocks!"}
{:animal "a quick happy brown hamster that rocks!"}
{:animal "a quick happy brown bird that rocks!"}
{:animal "a quick happy brown dog and fox that rock!"}
{:animal "a quick happy brown fish that rocks!"}]))
(def banned-from-house (atom ["fox" "bird"]))
最惯用的方法是什么?
此外,这个问题的标题是什么更好? (我在讨论 clojure 代码时很挣扎)
让我们一步步构建它。
首先,让我们使用 clojure.string/includes?
.
来测试一个字符串是否提到了一些动物的名字
(defn mentions-animal? [s animal]
(clojure.string/includes? s animal))
(mentions-animal?
"a quick happy brown fox that rocks!"
"fox")
=> true
(mentions-animal?
"a quick happy brown fox that rocks!"
"dog")
=> false
其次,让我们使用 clojure.core/some
.
来测试一个字符串是否提到了一系列动物名称的 some
(defn mentions-any? [s animals]
(some #(mentions-animal? s %) animals))
(mentions-any?
"a quick happy brown fox that rocks!"
#{"fox" "dog"})
=> true
(mentions-any?
"a quick happy brown fox that rocks!"
#{"cat" "dog"})
=> nil
接下来,将此逻辑扩展到动物图而不是字符串。
(defn animal-mentions-any?
[a animals]
(mentions-any? (:animal a) animals))
最后,使用clojure.core/remove
实现过滤逻辑:
(defn remove-banned-animals
[animals-list banned-animals]
(remove #(animal-mentions-any? % banned-animals) animals-list))
(remove-banned-animals
[{:animal "a quick happy brown fox that rocks!"}
{:animal "a quick happy brown hamster that rocks!"}
{:animal "a quick happy brown bird that rocks!"}
{:animal "a quick happy brown dog and fox that rock!"}
{:animal "a quick happy brown fish that rocks!"}]
["fox" "bird"])
=> ({:animal "a quick happy brown hamster that rocks!"} {:animal "a quick happy brown fish that rocks!"})
假设我想删除 banned-from-house collection:
中提及动物的列表元素(def list (atom [{:animal "a quick happy brown fox that rocks!"}
{:animal "a quick happy brown hamster that rocks!"}
{:animal "a quick happy brown bird that rocks!"}
{:animal "a quick happy brown dog and fox that rock!"}
{:animal "a quick happy brown fish that rocks!"}]))
(def banned-from-house (atom ["fox" "bird"]))
最惯用的方法是什么?
此外,这个问题的标题是什么更好? (我在讨论 clojure 代码时很挣扎)
让我们一步步构建它。
首先,让我们使用 clojure.string/includes?
.
(defn mentions-animal? [s animal]
(clojure.string/includes? s animal))
(mentions-animal?
"a quick happy brown fox that rocks!"
"fox")
=> true
(mentions-animal?
"a quick happy brown fox that rocks!"
"dog")
=> false
其次,让我们使用 clojure.core/some
.
(defn mentions-any? [s animals]
(some #(mentions-animal? s %) animals))
(mentions-any?
"a quick happy brown fox that rocks!"
#{"fox" "dog"})
=> true
(mentions-any?
"a quick happy brown fox that rocks!"
#{"cat" "dog"})
=> nil
接下来,将此逻辑扩展到动物图而不是字符串。
(defn animal-mentions-any?
[a animals]
(mentions-any? (:animal a) animals))
最后,使用clojure.core/remove
实现过滤逻辑:
(defn remove-banned-animals
[animals-list banned-animals]
(remove #(animal-mentions-any? % banned-animals) animals-list))
(remove-banned-animals
[{:animal "a quick happy brown fox that rocks!"}
{:animal "a quick happy brown hamster that rocks!"}
{:animal "a quick happy brown bird that rocks!"}
{:animal "a quick happy brown dog and fox that rock!"}
{:animal "a quick happy brown fish that rocks!"}]
["fox" "bird"])
=> ({:animal "a quick happy brown hamster that rocks!"} {:animal "a quick happy brown fish that rocks!"})