Skip to content

Instantly share code, notes, and snippets.

@Solonarv
Created November 20, 2018 15:51
Show Gist options
  • Save Solonarv/0f88493882c608f1cfe523111e295938 to your computer and use it in GitHub Desktop.
Save Solonarv/0f88493882c608f1cfe523111e295938 to your computer and use it in GitHub Desktop.
Generalization of 'when' to work with any Prism
{-# 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