Reverse 'exact' to better match sipure and common use cases. Add sireplicate_ and sisequence_.

This commit is contained in:
Paweł Nowak 2014-11-25 22:37:30 +01:00
parent 2fb082c6c1
commit a7986cc969
2 changed files with 14 additions and 8 deletions

View File

@ -220,15 +220,15 @@ morphed = iso morph morph
constant :: a -> SemiIso' () a
constant x = semiIso (\_ -> Right x) (\_ -> Right ())
-- | \-> Always returns the argument.
-- | \-> Filters out all values not equal to the argument.
--
-- \<- Filters out all values not equal to the argument.
exact :: Eq a => a -> SemiIso' () a
-- \<- Always returns the argument.
exact :: Eq a => a -> SemiIso' a ()
exact x = semiIso f g
where
f _ = Right x
g y | x == y = Right ()
f y | x == y = Right ()
| otherwise = Left "exact: not equal"
g _ = Right x
-- | Like 'filtered' but checks the predicate in both ways.
bifiltered :: (a -> Bool) -> SemiIso' a a

View File

@ -161,13 +161,19 @@ class SemiIsoMonad m => SemiIsoFix m where
{-# MINIMAL sifix | (=//=) #-}
-- | Equivalent of 'sequence'.
--
-- Note that it is not possible to write sequence_, because
-- you cannot void a SemiIsoFunctor.
sisequence :: SemiIsoApply f => [f a] -> f [a]
sisequence [] = sipure _Empty
sisequence (x:xs) = _Cons /$/ x /*/ sisequence xs
-- | Equivalent of 'sequence_', restricted to units.
sisequence_ :: SemiIsoApply f => [f ()] -> f ()
sisequence_ [] = sipure _Empty
sisequence_ (x:xs) = unit /$/ x /*/ sisequence_ xs
-- | Equivalent of 'replicateM'.
sireplicate :: SemiIsoApply f => Int -> f a -> f [a]
sireplicate n f = sisequence (replicate n f)
-- | Equivalent of 'replicateM_', restricted to units.
sireplicate_ :: SemiIsoApply f => Int -> f () -> f ()
sireplicate_ n f = sisequence_ (replicate n f)