I am new to Clojure and am still trying to wrap my head around doing things the "Clojure way". One function that I'm currently writing requires me to filter a collection (pile
) with several predicates, but the set of predicates to use depends on several function arguments (in this case ref-base
and single-ended?
). Here is the code I currently have:
(filter (every-pred
qcpass?
(case ref-base
"C" (if single-ended?
#(not (reverse? %))
#(and (properly-paired? %) (or (f1? %) (r2? %))))
"G" (if single-ended?
reverse?
#(and (properly-paired? %) (or (f2? %) (r1? %))))
nil?) ;; skip all piles if we're in a non-C/G position
) pile))
I feel that there is probably a better/more concise way to write this, since there is still some repetition of code in there, and a lot of parentheses. I'm also unhappy, aestetically, with the inline functions (#(..)
) to use and
/or
/not
within a filter. Could I ask for suggestions how to make this whole expression "prettier"?
question from:
https://stackoverflow.com/questions/65917627/simplify-a-filter-clause-with-many-predicates-that-depend-on-context 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…