Given the following filtering functions as unary predicates,
f1 :: Int -> Bool
f1 x = x > 30
f2 :: Int -> Bool
f2 x = x < 60
f3 :: Int -> Bool
f3 x = x `mod` 3 == 0
I'd like to filter a list of integers through all of them. Currently I'm doing something along the lines of:
filtered = filter f1 $ filter f2 $ filter f3 [1..90]
-- [33,36,39,42,45,48,51,54,57]
but it hardly feels like this is the most elegant solution possible; especially I don't like the multiple repetitions of filter
and the lack of composability.
Would there be a way to compose all these predicates into one, let's name it <?>
, so that a possible syntax would resemble something like the following?
filtered = filter (f1 <?> f2 <?> f3) [1..90]
-- [33,36,39,42,45,48,51,54,57]
The type signature of this hypothetical <?>
operator would then be (a -> Bool) -> (a -> Bool) -> (a -> Bool)
but I wasn't able to find any such thing on Hoogle.
filtered = (filter f1 . filter f2 . filter f3) [1..90]
filter
out in the expression you typed — but effectively, I didn't mention that in my question and this is already better thanfilter f1 $ filter f2 $ filter f3
... even though it's only replacing$
's by.
'sSemigroup
instance for predicates is essentiallyliftA2 (&&)
, as in your accepted answer.(&&)
to operate just on booleans). It has a function instance so you can simply writefilter (f1 && f2 && f3)
.