Skip to content

Instantly share code, notes, and snippets.

@blippy
Created November 18, 2015 18:44
Show Gist options
  • Save blippy/225fc9f309490e0a97fa to your computer and use it in GitHub Desktop.
Save blippy/225fc9f309490e0a97fa to your computer and use it in GitHub Desktop.
Haskell idiom for predicate lifting
Suppose I want to use an argument twice, as for example in the expression:
(\x -> (pred1 x) and (pred2 x))
Is there a shorter way of doing this?
liftA2 (&&) pred1 pred2
You can be cute and use the applicative instance on functions:
(&&) <$> pred1 <*> pred2
I would recommend against such cuteness however and just write out the arguments.
And here's the arrow implementation
(pred1 &&& pred2) >>> uncurry (&&)
You need Data.Tuple (for uncurry) and Data.Arrow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment