slack1
2018-4-20 14:05:10

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?


slack1
2018-4-20 14:05:33

(and tests every value against every boolean conditional, then returning pass/fail result)


greg
2018-4-20 14:31:17

Not that I know of, but it’s a “one-liner” using andmap and conjoin.


greg
2018-4-20 14:31:20

conjoin can and a list of predicate functions down to one.


greg
2018-4-20 14:31:27

andmap can apply that function to a list of values.


greg
2018-4-20 14:31:40

So (andmap (apply conjoin list-of-preds) list-of-values).


greg
2018-4-20 14:31:52

e.g. (andmap (apply conjoin (list number? even?)) (list 2 4))


greg
2018-4-20 14:32:03

You could define this as a little helper if you do it a lot.


greg
2018-4-20 14:33:49

@slack1 ^


greg
2018-4-20 14:36:11

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.


greg
2018-4-20 14:40:14

(In some langs or projects, something like the first way wouldn’t be “code-golf”, it would be “idiomatic”.)


slack1
2018-4-20 14:40:58

hmm for/and is indeed so much clearer


slack1
2018-4-20 14:41:23

but that’s partly because the conjoin procedure is unfamiliar to me