Created
November 20, 2018 15:51
-
-
Save Solonarv/0f88493882c608f1cfe523111e295938 to your computer and use it in GitHub Desktop.
Generalization of 'when' to work with any Prism
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE TemplateHaskell #-} | |
import Control.Lens.TH | |
import Control.Lens.Prism | |
whenP :: Applicative m => APrism s t a b -> s -> (a -> m ()) -> m () | |
whenP p s act = case matching p s of | |
Left _ -> pure () | |
Right a -> act a | |
whenP' :: Applicative m => APrism s t a b -> a -> m () -> m () | |
whenP' p s act = whenP p s (\_ -> act) | |
data Tree = Leaf | Node Tree Tree | |
$(makePrisms ''Tree) | |
errOnNil :: Tree -> Either String () | |
errOnNil tree = do | |
whenP' _Leaf $ Left "error: got a Nil!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment