module Protocol.Utils where import Control.Applicative (Applicative(..), Alternative(..)) import Control.Monad (Monad(..), join, mapM, unless, zipWithM) import Control.Monad.Trans.Class (MonadTrans(..)) import Data.Bool import Data.Either (either) import Data.Eq (Eq(..)) import Data.Foldable (Foldable, foldMap, and, sequenceA_) import Data.Function (($), id, const) import Data.Functor (Functor, (<$>), (<$)) import Data.Functor.Identity (Identity(..)) import Data.Functor.Compose (Compose(..)) import Data.Maybe (Maybe(..), maybe, fromMaybe) import Data.Ord (Ord(..)) import Data.Semigroup (Semigroup(..)) import Data.Text (Text) import Data.Traversable (Traversable(..)) import Data.Tuple (fst, snd, uncurry) import GHC.Natural (minusNaturalMaybe) import Numeric.Natural (Natural) import Prelude (fromIntegral) import Text.Show (Show(..)) import Control.Monad.Trans.Maybe (MaybeT(..)) import qualified Control.Monad.Trans.Except as Exn import qualified Control.Monad.Trans.State.Strict as S import qualified Data.ByteString as BS import qualified Data.List as List -- | NOTE: check the lengths before applying @f@. isoZipWith :: (a->b->c) -> [a]->[b]->Maybe [c] isoZipWith f as bs | List.length as /= List.length bs = Nothing | otherwise = Just (List.zipWith f as bs) -- | NOTE: check the lengths before applying @f@. isoZipWith3 :: (a->b->c->d) -> [a]->[b]->[c]->Maybe [d] isoZipWith3 f as bs cs | al /= List.length bs = Nothing | al /= List.length cs = Nothing | otherwise = Just (List.zipWith3 f as bs cs) where al = List.length as isoZipWithM :: Applicative m => m () -> (a -> b -> m c) -> [a] -> [b] -> m [c] isoZipWithM err f as bs = maybe ([] <$ err) sequenceA $ isoZipWith f as bs isoZipWith3M :: Applicative m => m () -> (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m [d] isoZipWith3M err f as bs cs = maybe ([] <$ err) sequenceA $ isoZipWith3 f as bs cs isoZipWith3M_ :: Applicative m => m () -> (a -> b -> c -> m d) -> [a] -> [b] -> [c] -> m () isoZipWith3M_ err f as bs cs = maybe err sequenceA_ $ isoZipWith3 f as bs cs