Is there a pre-existing Racket idiom or construct for a procedure that takes a list of boolean functions and a list of values to test?
(and tests every value against every boolean conditional, then returning pass/fail result)
Not that I know of, but it’s a “one-liner” using andmap
and conjoin
.
conjoin
can and
a list of predicate functions down to one.
andmap
can apply that function to a list of values.
So (andmap (apply conjoin list-of-preds) list-of-values)
.
e.g. (andmap (apply conjoin (list number? even?)) (list 2 4))
You could define this as a little helper if you do it a lot.
@slack1 ^
Having said that, I probably wouldn’t code-golf it. In real code I want to understand later, I’d probably use for/and
like: (for/and ([? (in-list (list number? even?))]
[v (in-list (list 2 4))])
(? v))
But that’s just my preference.
(In some langs or projects, something like the first way wouldn’t be “code-golf”, it would be “idiomatic”.)
hmm for/and is indeed so much clearer
but that’s partly because the conjoin procedure is unfamiliar to me