Clojure: Does parameter reversal contain? for use in condensate

I discovered this clojure problem today:

(condp contains? some-set "foo" "foo in thar" "bar" "bar in thar" "t'aint thar") 

The idea is to return a string under the first match, where some-set contains a value. If none of the values ​​are in the set, it returns the last value. The problem is that the contains? function contains? takes the collection first, then the key and condp needs the first key.

I β€œfixed” it by writing a function:

 (defn reverse-params [f] (fn [ab] (fba)) 

and substituting a challenge to him:

 (condp (reverse-params contains?) some-set "foo" "foo in thar" "bar" "bar in thar" "t'aint thar") 

Which works, but my question is: have I missed some better way to do this (possibly using some )? I could use cond , but I decided that it would save me some typing.

+5
source share
1 answer

No, you did not miss anything. This is a normal way to get around this. I often use an anonymous function for this if it will be used only once.

 (condp #(contains? %2 %1) some-set "foo" "foo in thar" "bar" "bar in thar" "t'aint thar") 

This problem also occurs when streaming macros -> and ->> . In such cases, I often use the as-> macro to give the stream value a name

+8
source

Source: https://habr.com/ru/post/1211211/


All Articles