Clojure:反转包含的参数?用于condp
我今天发现了这个clojure问题:Clojure:反转包含的参数?用于condp,clojure,Clojure,我今天发现了这个clojure问题: (condp contains? some-set "foo" "foo's in thar" "bar" "bar's in thar" "t'aint thar") 其思想是在第一个匹配项下返回字符串,其中some set包含一个值。如果集合中没有任何值,则返回最后一个值。问题是,contains?函数首先获取集合,然后获取密钥,condp首先需要密钥 我通过编写函数“修复”了它: (defn reverse-params [
(condp contains? some-set
"foo" "foo's in thar"
"bar" "bar's in thar"
"t'aint thar")
其思想是在第一个匹配项下返回字符串,其中some set
包含一个值。如果集合中没有任何值,则返回最后一个值。问题是,contains?
函数首先获取集合,然后获取密钥,condp
首先需要密钥
我通过编写函数“修复”了它:
(defn reverse-params [f] (fn [a b] (f b a))
并以电话代之以:
(condp (reverse-params contains?) some-set
"foo" "foo's in thar"
"bar" "bar's in thar"
"t'aint thar")
这是可行的,但我的问题是,我是否缺少更好的方法来实现这一点(可能通过使用
some
)?我可以使用cond
,但我想这样可以节省一些打字时间。不,你没有遗漏什么。这是正常的做法。
我经常为此使用匿名函数,如果它只使用一次
(condp #(contains? %2 %1) some-set
"foo" "foo's in thar"
"bar" "bar's in thar"
"t'aint thar")
当使用
->
和->
宏执行线程时,也会出现此问题。在这些情况下,我将经常使用as->
宏为线程值指定一个名称函数的通用名称是