Reputation: 54584
Consider
foldr (\x (a,b) -> (a || x==2, b || x==7 )) (False,False) [1..6]
--(True,False)
Ignoring the fact that this could be written easily using elem
, I have the strong feeling that I could employ Arrow
syntax to simplify the lambda, I just can't get it right.
Can this lambda be simplified using arrows? And do you have any general hints concerning how to "see" when arrows might work, and how to find the right expression?
Upvotes: 3
Views: 340
Reputation: 89093
Pull the computation out of the foldr -
ghci> :m +Control.Arrow
ghci> any (==2) &&& any (==7) $ [1..6]
(True,False)
But if you want to be sure you're only traversing the list once, try using the bifunctor package:
ghci> :m +Data.Bifunctor +Data.Bifunctor.Apply
ghci> foldr (bilift2 (||) (||) . ((==2) &&& (==7))) (False, False) [1..6]
(True,False)
Upvotes: 7
Reputation: 12010
foldr (\x -> (|| x==2) *** (|| x==7)) (False,False) [1..6]
I don't think you can abstract the x
out with arrows.
Edit: well, seems like you can:
foldr (uncurry (***) . (((||) . (==2)) &&& ((||) . (==7)))) (False,False) [1..6]
Upvotes: 6