A Predicate Combinator

I’ve experimented a lot with PLT SchemeRacket lately. I found myself testing values against several predicate functions a couple of times and writing this kind of code:

(define (integer-or-string? x) (or (integer? x) (string? x)))

Not too pretty. Inspired by Racket’s contract combinator or/c I wrote this:

(define ((or/p . ps) x) (ormap values (map (lambda (f) (f x)) ps)))

And now I can write this:

(define integer-or-string? (or/p integer? string?))

And then I lived happily ever after.

Update: Prior art: disjoin in cce/scheme


For comparison, the function would be written like this in Haskell:

or_p ps x = or (map ($x) ps)

(or is a function in Haskell, unlike Racket where it is a macro, because it can be short-circuiting anyway because of lazy evaluation.)